Re:How Lisp macros differ from static code-generation and me...
by funptr on Wednesday January 26, 2011 @03:25PM
(#2144979)
>Is there a C macro that can do this?
No, of course. C macros only see strings; you need to parse a syntax tree to do your reverse example (like lisp macros).
No argument from me.
>Lisp's macro system allows you to create new syntax, including changes in flow control.
Well, C macros seem to be able to manipulate control flow. You can't break up an expression to do that (not smart enough to parse), but you can rearrange expressions that are given whole.
Re:How Lisp macros differ from static code-generation and me...
by draegtun on Sunday January 30, 2011 @08:02AM
(#2157961)
Wonderful example of what macros can do.
Just for shit & giggles I'd thought I give it a go in Io (http://www.iolanguage.com):
executeInReverse := method (
m := call argAt(0)
stmts := list() // list of statements (ie. messages)
loop (
rest := m next // rest of messages after current
stmts append(m setNext) // get current message
m := rest
if (m == nil, break) // exhausted statements when "nil"
)
stmts reverseForeach (n, doMessage(n))
)
executeInReverse( writeln("Hi") writeln("Middle") writeln("Bye") )
And I think with a bit more work I can get it to amend its AST rather than rerunning the loop each time it sees executeInReverse.
But my example is trivial, too. I could go on to swap individual parts of my forms around, remap them to other forms depending on various conditions, etc.
In addition, people often talk about Lisp macros, but they neglect to mention the power of reader macros, which allow you to go beyond Lisp's basic AST look-and-feel.