Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

JavaScript is not a homoiconic language, because the important feature of homiconicity is not that the syntax "mirrors" the syntax of data, but that the syntax is actually represented as a piece of data that can be manipulated in the language.

For example, in Scheme, I can easily generate data structures and interpret them as code to be executed or as data to be manipulated. For example, here I can generate code corresponding to a given factorial, and also muck with it.

    > (define (fact-code n)
        (if (= n 0) 1 `(* ,n ,(fact-code (- n 1)))))
    > (fact-code 4)
    (* 4 (* 3 (* 2 1)))
    > (eval (fact-code 4) (the-environment))
    24
    > (eval (replace '* '+ (fib-code 4)) (the-environment))
    10
We can write a function similar to fact-code in Rebol, which I'm borrowing from an earlier comment[^1]

  >> fact-code: func [n] [either n = 1 [1] [compose [(n) * (fact-code n - 1)]]]
  
  >> fact-code 4
  == [4 * 3 * 2 * 1]
  
  >> do fact-code 4
  == 24

  >> do replace/all fact-code 4 '* '+
  == 10
There is no analogous way of generating and manipulating JavaScript code using JavaScript. If all JS code could be understood as JSON that then could be fed back into JavaScript and manipulated there, then it would be homoiconic.

[^1]: Borrowed from this older comment with a minor name change https://news.ycombinator.com/item?id=5809980



My mistake, thank you for the correction.

What made me believe JS / JSON were homoiconic was down to the following vulnerable method of loading a JSON file:

    var obj = eval (json);
But after reading your post, I can see how this differs from homiconicity




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: