3 ms·
If one is looking for degrees of homoiconicity, a useful question to ask is how hard it is for a program to modify its code and execute the result. In Lisp, it'
by johan_larson 12y ago
If one is looking for degrees of homoiconicity, a useful question to ask is how hard it is for a program to modify its code and execute the result. In Lisp, it's very easy, so Lisp is meaningfully homoiconic.
For a C program to modify its code, it needs access to its source code (which it might not have) and a compiler (also not necessary) and the programmer has to write rather contrived code to modify the source, compile it, fork, and start executing the result. So C is not meaningfully homoiconic.
Or I suppose the C program could modify an executable directly, but that is even more contrived since it requires the programmer to work with a completely different representation (machine code) than the language of the original program.
- lispm 12y agoThat has little to do with homoiconicity. What you wrote about is called 'reflection' https://en.wikipedia.org/wiki/Reflection_(computer_programming) https://en.wikipedia.org/wiki/Reflection_(computer_programmi... > reflection is the ability of a computer program to examine (see type introspection) and modify the structure and behavior (specifically the values, meta-data, properties and functions) of the program at runtime. Ignore the rest of the Wikipedia page, it is not really useful, since the examples show only very simple reflection capabilities.
- taeric 12y agoTo be fair. The point, I believe, is that for a lisp program to alter itself, it only needs access to its source code. Which is also all it really needs to execute itself. More impressively, the modified lisp program could easily serialize itself out for study and modification. Languages that use reflection can't easily do this.
- Someone 12y agoThat wikipedia definition, IMO, is weird. I would limit reflection to modifying values, metadata and properties, not to modifying functions. Limiting it to that is what allows strongly-typed somewhat ahead of time compiled languages such as Java and C# to have reflection. And yes, C# has support for creating new functions at runtime, but that is not reflection, not in my book, and not in Microsofts's (if it was, they would have put it in the reflection namespace) Modifying code is a step up. For example, in lisp, it is fairly easy to grab a function, walk its tree for use of integers, and replace every '3' by '4' and every usage of the '+' function by '-' And the wikipedia examples are amazingly bad, indeed. eval, as in the php example, has nothing to do with reflection. Neither does Objective-C's performSelector. You want to show respondsToSelector; that's what does the introspection part.
- lispm 12y agoIn Lisp it is relatively easy to change a function. A typical thing is 'advising', where some code is added to a function. This had been done to ordinary functions decades ago and Common Lisp added it as a standard to its object system. > Modifying code is a step up. For example, in lisp, it is fairly easy to grab a function, walk its tree for use of integers, and replace every '3' by '4' and every usage of the '+' function by '-' Actually it is not easy. Actually walking Lisp code is relatively complex. The code walker needs to understand built-in Lisp constructs, macros and more.