Alex Rivera | Logout

How are Clojure macros different from C macros?

Asked 2013-07-08T12:03:19.283
12

I am new to Clojure. I started couple of months ago. I am trying to learn Macros.

I initially got confused understanding difference between macros and higher-order functions in Clojure as a higher order function could take lambdas and execute one of them how many times it wanted based on any conditions and filter.

So I posted a question with a simple example regarding this on StackOverflow itself. I got my doubts cleared from the answer.

This is what I understood,

  • Macros won't evaluate all the arguments unlike functions to evaluate the body.
  • Macros can be selective about what to evaluate and what not and how to convert one piece of code into another using quote,unquote and splicing syntax.
  • The final code that comes out of the macro is then evaluated.

So my question is, How is it different from preprocessor directives and macros used in C ? What power does Lisp/Clojure macros give to the developers which C macros completely lack and are often used widely.

Edit
Report

1 Answer

13

Some notable differences:

  • Clojure macros operate on Lisp data structures whereas C macros operate on text. This capability is a consequence of Lisps being homoiconic (i.e. Lisp source code is expressed as Lisp data structures). Homoiconicity isn't strictly necessary for a macro system to be effective, but it certainly makes it much more convenient and natural.
  • You can execute Clojure macros at runtime as well as compile time (e.g. using eval)
  • Clojure macros are written in Clojure itself. Contrast with C preprocessor macros, which have their own separate mini-language. This is a big advantage when writing more complex macros: you don't have to mentally switch between different languages (you do of course still need to mentally distinguish between which code you want to execute while processing the macro vs. which code you want to generate as the output of the macro)
  • Clojure macros are Turing complete - you can perform arbitrary code generation within them. Not true for standard C preprocessor macros, which are somewhat limited in their ability to express complex code generation. EDIT: Thanks Jeremy for providing links to some amusing hacks by which the C preprocessor can be coerced into acting in a turning-complete manner. The overall point still stands though: these aren't really practical ways to write general purpose code.

Arguably, macros are still the distinguishing "killer feature" of Lisps. For a little extra explanation on this topic, it's worth reading Paul Graham's essay "What Made Lisp Different"

answered 2013-07-08T12:13:44.053

Your Answer