3 ms·
Real world examples using homoiconicity?
I'm interested in example code that uses homoiconicity well. I'm not interested in pedagogical examples, such as the excellent and oft cited meta-circular evaluator.
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.
- pavelludiq 16y agoI only know lisp, so I'll use it as an example, but there are other homoiconic languages out there. 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).
- jasonwatkinspdx 16y agoThese are great teaching examples, but what I haven't seen yet is how it can be used solving problems or implementing algorithms. I'm very curious what such code looks like and how homoiconicity is leveraged to advantage.
- pavelludiq 16y agoOk then, check out this awesome DSL example screencast in common lisp: http://lispm.dyndns.org/news?ID=NEWS-2005-07-08-1 http://lispm.dyndns.org/news?ID=NEWS-2005-07-08-1 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/SwineBeforePerl/ 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.
- jasonwatkinspdx 16y agoThanks, I'll check these out.