4 ms·
I 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
by pavelludiq 16y ago
I 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.