Hacker News new | ask | show | jobs
by andolanra 4764 days ago
Homoiconicity is when the code of a language is represented in a structure that is primitive to the language. It doesn't have to do with how data is stored, it has to do with how the program text itself is stored.

The advantage of homoiconicity is that the language itself is manipulable as a basic type in the language. In Scheme, this means that it's easy to generate, analyze, and modify Scheme code using simple procedures in Scheme. e.g. I could (although I wouldn't) generate a scheme expression to compute an arbitrary fibonacci number with[1]

    > (define (fib-code n)
        (if (= n 1) 1 `(* ,n ,(fib-code (- n 1)))))
    > (fib-code 4)
    (* 4 (* 3 (* 2 1)))
    > (eval (fib-code 4) (the-environment))
    24
    > (eval (replace '* '+ (fib-code 4)) (the-environment))
    10
Notice that I'm not just pushing strings together to write code. I'm actually manipulating data structures in the language to produce and modify code. That is homoiconicity.
1 comments

Nice example, but (nitpick warning) that's not a Fibonacci (http://oeis.org/A000045) computation. It computes triangular numbers (http://oeis.org/A000217)