Tuesday, 28 May 2013

When would I require a Macro instead of a function?

When would I require a Macro instead of a function?

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 ?

No comments:

Post a Comment