A macro definition is very similar to a function definition. However, when a function is called, it returns a value; when a macro is called, it returns code, which is then executed. Macros let you "extend the language," and are widely considered (by Lisp enthusiasts) to be what makes Lisp so great.
(match value & expr), where there are an
even number of expressions.value may be any Clojure value, including function calls.
It is simply copied into the expanded code.expressions consists of (1) a test applied to the
value, and (2) an expression to be evaluated and returned if the test
passes. Each test may be:pos? -- in this case,
the test should apply the function to the value, as (pos? value).f is a function with (function?
f), but first you have to (ns user (:use
clojure.test)). (>
0) -- in this case, the value should be inserted as the second
element, as (> value 0). :else, which in a cond is
equivalent to true.match expression can be translated into a cond
expression in a pretty straightforward way:| For example, | Should expand to: |
|
|
Turn in
match macro, and
using it in two or three examples.readme.txt, describing how to use your
macro, and giving an example use and an example macro expansion.