Skip to content

Instantly share code, notes, and snippets.

@madstap
Created May 12, 2016 19:08
Show Gist options
  • Save madstap/c93305a09fecf552b346f21a49148b4d to your computer and use it in GitHub Desktop.
Save madstap/c93305a09fecf552b346f21a49148b4d to your computer and use it in GitHub Desktop.
;; The code starts as a string in a text file, like any programming language.
"(defn f [x]
(* x x))"
;; Then it's read by the reader.
(with-in-str
"(defn f [x]
(* x x))" (read))
;; Read returns this data structure:
'(defn f [x]
(* x x))
;; Which is a list of 4 elements,
;; Two symbols: defn and f
;; A vector which contains a symbol: [x]
;; And a list with three symbols: (* x x)
;; The function eval then transforms this data structure to jvm bytecode
(eval '(defn f [x]
(* x x))) ;;=> #'user/f
;; A nice consequence of this is that it allows macros
;; Which can be thought of as functions that take a code data structure
;; and returns another data structure that is then eval'd
(defmacro rev [form]
(reverse form))
(rev (2 3 *)) ;;=> (* 3 2) ;;=> 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment