Pergunta

It seems that sometimes code in functional programs accepts other code (functions) as arguments and modifies it and returns it for execution. It seems similar to self-modifying code. Does this mean that the compactness of functional programs result from the same reason that allows extremely compact self-modifying programs?

Foi útil?

Solução

Ernest Friedman-Hill may be correct that this question doesn't belong here, but here is an answer:

I would say: No.

There are various ways that a language can allow self-modifying code. The most flexible way would be to be able flip arbitrary bits at arbitrary locations in memory. (Here is a classic story: The Story of Mel.) But self-modifying methods need not exhibit such dangerous freedom. In Common Lisp, it's easy to write code that constructs Lisp code and the evaluates it, but this all stays within the constraints of Lisp, and it's not necessarily more compact. In Java, you can do certain kinds of "self-modification" using reflection, but this is often more verbose than regular Java usage.

Also, keep in mind that there's actually a fine line between some kinds of object-oriented methods and functional programming methods. Both allow you to pack up executable things and data together. OO does it by attaching methods to objects, while functional languages do it by using closures to capture data. Thus, if functional languages are self-modifying, Java is too. But Java is not normally thought of as a self-modifying language (except for reflection), and Java code is often verbose. (By the way, Guy Steele, one of the designers of Java, was also one of the people who designed the Lisp dialect Scheme--a more purely functional language than Common Lisp--in order to experiment with object-oriented programming! I believe that the idea was to construct different object-oriented language constructs using closures.)

Caveat: I haven't done any hard-core Java programming in a while, and there are probably language features relevant to this issue of which I'm unaware.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top