The Homoiconic Advantage

So there’s our answer: Clojure’s syntax is the way it is because it’s amphibious, equally at home representing code and data. Having a single text format along with a single in-memory representation of both code and data is not just elegant; it also has some serious practical advantages. For example, writing Clojure code-analysis tools is very straightforward. Need to read a file full of Clojure code? No problem:

 (ns codetool.core
  (:require [clojure.java.io :as io]))
 
 (​defn​ read-source [path]
  (with-open [r (java.io.PushbackReader. (io/reader path))]
  (loop [result []]
  (​let​ [expr (read r false :eof)]
  (​if​ (= expr :eof)
  result
  (recur (conj result expr)))))))

Call read-source with the path to a Clojure ...

Get Getting Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.