Hacker News new | ask | show | jobs
by Androsynth 4761 days ago
javascript has json though, which doesnt make it homoiconic, but it allows you to mimic homoiconicity in a lot of cases. When I write in javascript, I tend to store my data in json and and write code that is generic, with much abstraction that has a small foot print to manipulate the json.

That is scheme-like. That is psuedo-homoiconic.

(If anyone in the know wants to add or refute this, I would love to know whether my non-cs intuition is correct here.)

(also it is still a very mutable language, even with coffeescript/underscore)

2 comments

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.
Nice example, but (nitpick warning) that's not a Fibonacci (http://oeis.org/A000045) computation. It computes triangular numbers (http://oeis.org/A000217)
JSON was heavily inspired by Rebol - http://www.rebol.com/article/0423.html

However unlike JSON, Rebol is more than just a data interchange format because it's like Lisp in being a full homoiconic programming language.

For eg: Here are andolanra's excellent Scheme/Lisp examples in Rebol:

  >> some-code: func [n] [either n = 1 [1] [compose [(n) * (some-code n - 1)]]]
  
  >> some-code 4
  == [4 * 3 * 2 * 1]
  
  >> do some-code 4
  == 24
  
  >> do replace/all some-code 4 '* '+
  == 10
And it's this that makes the difference between JSON+Javascript compared to something like Rebol/Scheme/Lisp.