8

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
  • 3
    I don't have a specific example for you, but the simple answer is that macros are needed for creating new syntax (as in Domain-Specific Languages). But, as your example illustrates, ordinary first-class Lisp functions are already powerful enough to do the job without requiring macros, in many cases. You should favor ordinary functions over macros; only use macros when an ordinary function won't do. May 28, 2013 at 14:38
  • 1
    See also stackoverflow.com/questions/2561221 May 28, 2013 at 14:52

3 Answers 3

6

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
  • 1
    I get it. becoz of my mind, I cant think beyond a function. i always end up making a construct that looks like a function and can be done with a function. because I am used to writing code not making languages and constructs or DSls. May 29, 2013 at 12:34
2

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)
-2

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
  • 3
    This doesn't really answer the main question. It only nitpicks on the example. May 28, 2013 at 14:31
  • 1
    The last part answers the question. It says something a macro can do that a function cannot.
    – WuHoUnited
    May 28, 2013 at 22:10
  • You could have been a little clearer about how argument evaluation differs with macros. May 28, 2013 at 22:46
  • I was learning macros. I wanted to create a construct assuming it wasn't there. So I thought of making a switch case using if form. I couldn't think of anything else. May 29, 2013 at 13:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.