Facebook badge

Amol Aggarwal's Facebook profile

About Me

I like to write once in a while. I hope you will like my blog.

Friday, December 19, 2008

c vs lisp macros

Thanks to Kevin Clancy for his post .
Original link

Lisp macros
A C preprocessor operates on source tokens. A Lisp macro
operates on the abstract syntax tree produced by parsing the source
tokens. A C preprocessor can replace a token (or a simple pattern of
tokens) with some other tokens. A Lisp macro can perform arbitrary
computations on the input syntax tree, using the full power of the
language and runtime, and the input it operates on is a structured
syntax tree represented as a Lisp list, not a stream of characters or
tokens. You could in theory make a C preprocessor do anything that can
be done in C, but if you want to go beyond what the typical preprocessor
does, you pretty much have to write a new preprocessor. Lisp's macro
facility is out of the box able to easily do anything to the input tree
that Lisp can do to lists, which is quite a lot.
A macro merely takes one sexp (which we think of as "code") and
transforms it into another sexp. Ever notice how you don't perceive
sourcecode character-by-character? You mentally see it as symbols,
numbers, etc. Well, after lisp slurps in the characters of your
sourcecode and turns it into sexps, you can conveniently manipulate or
analyze this code just like any sort of data.
Lisp macros rulezzz.