I am new to Clojure, I am new to Macros and I have no prior background in Lisp.
I went on to create my own switch case like form and ended up with this:
(defmacro switch-case [v cases default] (if (cases v) (cases v) default ))
and then tried making a function and ended up with this:
(defn fn-switch-case [v cases default] (if (cases v) (cases v) default ))
Both
(switch-case 5 {6 "six" 7 "seven"} "not there")
and
(fn-switch-case 5 {6 "six" 7 "seven"} "not there")
Work fine.
What could be the scenario where I would need a macro and a function wont work ?
Is there a drawback in my function or macro implementations ?
2
One benefit of macros is that they don’t follow the same evaluation rules as functions, since they’re just performing transformations on the code.
An example of a construct you couldn’t create just by using functions is Clojure’s threading macro.
It lets you insert an expression as the first argument in a sequence of forms:
(-> 5
(+ 3)
(* 4))
is equivalent to
(* (+ 5 3) 4)
You wouldn’t be able to create this kind of construct using only functions, since before the ->
expression was evaluated, the inner (+ 3)
and (* 4)
would be, meaning ->
would receive
(-> 5
3
4)
and it needs to see the actual functions being used in order to work.
In your example, consider if the result for some case were to have side-effects or call some other function. A function version would not be able to prevent that code from being run in a situation where that result is not picked, whereas a macro could prevent that code from being evaluated unless it is the option taken.
1
One fun aspect of macros are that they give you the possibility of expanding the syntax of your lisp and adding new syntactic features to it, and this happens only because arguments passed to a macro will get evaluated only at runtime, and not at the time of compiling your macro. As an example the first/last thread macros in clojure -> ->>
don’t evaluate their expression-arguments, otherwise these evaluated results couldn’t accept anything more (say (+ 3) => 3
and 3
is not a function which could accept your first main argument in something like (-> 4 (+ 3))
).
Now if i like this syntax and i want to add it to my Common Lisp implementation (which doesn’t have it) i can add it up by defining a macro myself. Something like this:
;;; in SBCL
;;; first thread:
(defmacro -> (x &rest more)
(loop for m in more
for n = `(,(first m) ,x ,@(rest m)) then `(,(first m) ,n ,@(rest m))
finally (return n)))
;;; last thread:
(defmacro ->> (x &rest more)
(loop for m in more
for n = `(,(first m) ,@(rest m) ,x) then `(,(first m) ,@(rest m) ,n)
finally (return n)))
Now i would be able to use them in Common Lisp the same way as in Clojure:
(-> #'+
(mapcar '(2 3 4) '(1 2 3))) ;; => (3 5 7)
(->> #'>
(sort '(9 8 3 5 7 2 4))) ;; => (9 8 7 5 4 3 2)
Also maybe you want to have a new syntax for clojure’s range
function with your own keywords for your syntax, something like:
(from 0 to 10) ;=> (0 1 2 3 4 5 6 7 8 9)
(from 0 to 10 by 0.5) ;;=> (0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5)
can be defined like this macro:
(defmacro from
"Just another range!"
[x y z & more]
`(when (= '~y '~'to)
(if '~more (range ~x ~z (second '~more))
(range ~x ~z))))
or add something similar to Common Lisp (it is only for example since all these can already be done in the languages of course!):
(defmacro from (x y z &rest r)
`(when (eql ',y 'to)
(if (and ',r (eql (first ',r) 'by))
(loop for i from ,x to ,z by (second ',r) collect i)
(loop for i from ,x to ,z collect i))))
(from 0 to 10) ;=> (0 1 2 3 4 5 6 7 8 9 10)
(from 0 to 5 by 1/2) ;=> (0 1/2 1 3/2 2 5/2 3 7/2 4 9/2 5)
I’m not exactly sure what you want your switch-case function to do. What should be the value of:
(def x 5)
(switch-case x {5 :here}
:not-there)
It might be useful if you viewed the source of the related core functions/macros by using the repl
(clojure.repl/source case)
But in general a reason why you might want a macro as opposed to a function is that using the core case
macro
(case 6
5 (print "hello")
"not there")
will not print hello.
However, if case
were defined as a function, it print “hello” to the screen and the whole expression would evaluate to "not there"
. This is because functions evaluate their arguments before every calling the function.
4