Real world examples using homoiconicity?
I'd also be interested to hear the opinions of lisp users and the like on the benefits of homoiconic code vs the risks of using eval() or the equivalent.
I'd also be interested to hear the opinions of lisp users and the like on the benefits of homoiconic code vs the risks of using eval() or the equivalent.
In lisp i can do this:
(eval (cons '+ '(1 2 3)))
What i did was that i added the symbol + to the front of the list (1 2 3) to get (+ 1 2 3) which is a valid lisp expression, and i eval-ed it resulting in 6.i can do other such things:
(eval (reverse '(3 2 1 +)))
I take the list (3 2 1 +) which is an invalid lisp expression, and i reverse it, giving me (+ 1 2 3).See what we did here? We took a datastructure and we transformed it into something the lisp compiler can recognize as a valid form. We can do this because lisp code is expressed as lisp datastructures(thats what homoiconic means).
I don't know how much common lisp you know, but you asked for a real example.
Also check out this awesome talk by Shriram Krishnamurthi: http://www.cs.brown.edu/~sk/Publications/Talks/SwineBeforePe... (audio and slides). It uses scheme. There are many more examples, but these are just off the top of my head.