07 Macros
07 Macros
1 Material
• 17_homoiconicity.clj
• 11_macros.clj
• Clojure for the Brave and True, chapter 8 (contains next week’s material as well)
2 Learning Outcomes
After completing this unit you should be able to
3 Highlights
• Macroexpension
• Macro: defmacro
4 Exercises
Note: the next reading guide will contain the same exercises. You may — for didactical purposes — try
these exercises now to experience the pain of writing macros without the templating syntax.
Exercise 7.1 (Macro)
a) Write a macro that implements a simple calculator with infix notation. The first argument should be
a vector with bindings of symbols to values. Parentheses and operator precedences need not be
considered. The macro should work as follows:
user=> (calc [a 3] 3 + 5 * a)
24
user=> (calc [b 5 a 3] a * b + 3)
18
(declare z0 z1 z2 z3 z4)
(def z0 (dfa-state :accept {\0 z0 \1 z1}))
(def z1 (dfa-state :reject {\0 z2 \1 z3}))
(def z2 (dfa-state :reject {\0 z4 \1 z0}))
(def z3 (dfa-state :reject {\0 z1 \1 z2}))
(def z4 (dfa-state :reject {\0 z3 \1 z4}))
Nota bene: You have to use the code snippet above verbatim!
No modifications are allowed.
(dfa-state ... ) should return a function, which receives a
string, consisting of 0s and 1s, as parameter and returns :ac-
cept, if the string starting from this node results in an accepting
state. Otherwise :reject should be returned.
(dfa-match init text) should serve as entry point, which, starting from init, processes the input
text.