Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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 ?

share|improve this question
1  
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. –  Robert Harvey May 28 '13 at 14:38
    
See also stackoverflow.com/questions/2561221 –  Robert Harvey May 28 '13 at 14:52
add comment

2 Answers

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.

share|improve this answer
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. –  Amogh Talpallikar May 29 '13 at 12:34
add comment

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.

share|improve this answer
2  
This doesn't really answer the main question. It only nitpicks on the example. –  Florian Margaine May 28 '13 at 14:31
1  
The last part answers the question. It says something a macro can do that a function cannot. –  WuHoUnited May 28 '13 at 22:10
    
You could have been a little clearer about how argument evaluation differs with macros. –  Robert Harvey May 28 '13 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. –  Amogh Talpallikar May 29 '13 at 13:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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