Clojure Macros Homoiconicity n All versions of Lisp

  • Slides: 14
Download presentation
Clojure Macros

Clojure Macros

Homoiconicity n All versions of Lisp, including Clojure, are homoiconic n n Original Lisp

Homoiconicity n All versions of Lisp, including Clojure, are homoiconic n n Original Lisp used only lists and atoms n n “atom”: A simple value (number, string, nil) Clojure adds vectors, maps, and a few other things n n n This means that there is no difference between the form of the data and the form of the program The result is still homoiconic, even if it’s less obviously true The compiler mostly compiles everything down into a common form anyway Homoiconicity greatly simplifies metaprogramming 2

Macros, defined n Metaprogramming is writing code that produces code n Metaprogramming is particularly

Macros, defined n Metaprogramming is writing code that produces code n Metaprogramming is particularly easy in the Lisp family of languages, because of homoiconicity n n n “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 n n n Arguments to a macro are not evaluated Macro calls are evaluated at compile time The return value of a macro should be executable code 3

A trivial macro: triple-do n (defmacro triple-do [form] (list 'do form) ) n n

A trivial macro: triple-do n (defmacro triple-do [form] (list 'do form) ) n n n 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")) n n Result: The list (do (println "Hello")) If executed from the REPL, the result is Hello nil 4

Why macros? n When you write any reasonably sized program, you are designing a

Why macros? n When you write any reasonably sized program, you are designing a language to solve a particular class of problems n n Your functions are the “verbs” in the language Your variables are the “nouns” in the language Your “grammar” is imposed by the language—if statements, while loops, and so on Metaprogramming allows you to define new “grammar” for your language 5

Abstract Syntax Trees n n For virtually every programming language, a compiler or interpreter

Abstract Syntax Trees n n For virtually every programming language, a compiler or interpreter parses programs into an abstract syntax tree In Lisp, the AST is represented directly! (if (> x y) (reset! max x) (reset! max y)) 6

Syntax n In a sense, Lisp (and Clojure) are “syntax free” n n There

Syntax n In a sense, Lisp (and Clojure) are “syntax free” n n There is syntax, but it directly represents the abstract syntax tree You can define new functions, but you cannot define new special forms Macros give you a power equivalent to writing new special forms Because of homoiconicity, Lisp macros are far more powerful that “macros” in C 7

Quotes and unquotes n n n The usual way of quoting something is to

Quotes and unquotes n n n 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 n n Quotes and unquotes can be nested, to any level The following are equivalent: n n (defmacro triple-do [form] (list 'do form) ) (defmacro triple-do [form] `(do ~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 8

Splicing unquotes n n n println can take multiple arguments, which it prints in

Splicing unquotes n n n 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 n n (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 n (defmacro rev-println [args] `(println ~@(reverse args)) ) 9

Generating symbols n Just as in a function, you can use let to define

Generating symbols n Just as in a function, you can use let to define the names of local variables n n n 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 n n Clojure will replace these names with unique, generated names Example: A println macro that also returns the value printed n (defmacro debug-println [expr] `(let [result# ~expr] (println (str “Value is: “ result#)) result# ) ) 10

Debugging n n To see what you get as the result of expanding a

Debugging n n To see what you get as the result of expanding a macro, use macroexpand Example: (macroexpand '(triple-do (println "Hello"))) gives (do (println "Hello")) 11

Why not macros? n n n Macros are a higher level of abstraction that

Why not macros? n n n Macros are a higher level of abstraction that the usual features of a programming language This makes them more powerful, but also harder to reason about Don’t kill flies with sledgehammers! 12

When to use macros n General rule: If you can do it with a

When to use macros n General rule: If you can do it with a function, do it with a function n n 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 13

The End 14

The End 14