Clojure Macros

Download Report

Transcript Clojure Macros

Clojure Macros
Macros, defined

Metaprogramming is writing code that produces code

Metaprogramming is particularly easy in the Lisp family of
languages, because of homoiconicity



“All code is data, and all data is code”
In Lisp languages, macros are the primary means of
doing metaprogramming
A macro definition is like a function definition, with
three important differences



Arguments to a macro are not evaluated
Macro calls are evaluated at compile time
The return value of a macro should be executable code
2
A trivial macro: triple-do

(defmacro triple-do [form]
(list 'do form form form) )



The do is quoted, so it is put as is into the result list
Three copies of the value passed in to the form are put into the list
(triple-do (println "Hello"))


Result: The list
(do (println "Hello")
(println "Hello")
(println "Hello"))
If executed from the REPL, the result is
Hello
Hello
Hello
nil
3
Quotes and unquotes



The usual way of quoting something is to put a single quote mark
in front of it: '(a b c)
The backquote does the exact same thing: `(a b c)
However, things within a backquote can be “unquoted” and
evaluated, by putting a tilde, ~, in front of them


Quotes and unquotes can be nested, to any level
The following are equivalent:




(defmacro triple-do [form]
(list 'do form form form) )
(defmacro triple-do [form]
`(do ~form ~form ~form) )
The second of these is called a “template”
For complex macros, templates can be a lot easier to read, because they
“look like” the code that is generated
4
Splicing unquotes



println can take multiple arguments, which it prints in order
Suppose you wanted to write a macro that prints its arguments
in reverse order
You might try




(defmacro rev-println [args]
`(println ~(reverse args)) )
Given a list of values, this will print them as a list
(rev-println `(a b c)) would print (c b a), not c b a
The splicing-unquote operator, ~@, will insert the list
values individually, not as a list

(defmacro rev-println [args]
`(println ~@(reverse args)) )
5
Generating symbols

Just as in a function, you can use let to define the names of local
variables



These names can be used in the generated code, where they will appear as
written
However, the names in the generated code may conflict with other names
in use where the macro is called
To avoid this issue: in any syntax-quoted form (forms using the
back tick), add the # symbol to the end of any local names


Clojure will replace these names with unique, generated names
Example: A println macro that also returns the value printed

(defmacro debug-println [expr]
`(let [result# ~expr]
(println (str “Value is: “ result#))
result# ) )
6
Debugging


To see what you get as the result of expanding a macro, use
macroexpand
Example:
(macroexpand '(triple-do (println "Hello")))
gives
(do (println "Hello") (println "Hello")
(println "Hello"))
7
When to use macros

General rule: If you can do it with a function, do it
with a function


Macros are easy to describe but difficult to reason about
The primary thing that macros do for you is allow you
to modify the language by creating new control
structures and formalizing recurring patterns
8
The End
9