Re: eval in bash vs macro in lisp
- From: Ari Johnson <iamtheari@xxxxxxxxx>
- Date: Sat, 26 Aug 2006 01:38:51 -0400
Weiguang Shi <wgshi@xxxxxxxxxxxxxxxxxxxxx> writes:
Thank you! I could only suspect.
So although I don't understand LISP macros, I can still grok it.
Off-topic note: The verb "to grok" comes from Heinlein's fascinating
novel _A Stranger in a Strange Land_, and roughly means to have such
an intimate understanding of as to be nearly one with it. If you
don't understand something, you cannot grok it.
But back to the topic itself, macros can't be fairly analogized to any
other language that I know of, because they allow you to add syntactic
features to the language. I try explaining this by implementing a
'for' loop in Common Lisp using a macro. Common Lisp does not have
such a feature, so adding it with a macro is a substantial change to
the syntax of the language.
In C, you write:
for (i = 0; i < 10; i++) {
printf("i = %d\n", i);
}
In Lisp, this would look like:
(for (i 0) (< i 10) (incf i)
(format t "i = ~D~%" i))
If you type that into your Lisp, you will get an error or two. Those
errors will include the fact that there's no such function I and the
fact that there's no such function FOR.
Let's add it to the language:
(defmacro for (init continue next &body body)
(unless (consp init)
(error "Syntax error - init form must be a list"))
(let ((looplabel (gensym))
(var (first init))
(initval (second init)))
(unless (symbolp var)
(error "Syntax error - variable must be a symbol"))
`(prog ((,var ,initval))
,looplabel
,@body
,next
(when ,continue
(go ,looplabel)))))
Now try that example form again, and it will print out what you'd
expect. Lisp did not have this syntax beforehand and it does now.
You can't do that in bash. Just because macros can do one thing that
you can do with eval in bash doesn't mean that eval in bash can do
other things that macros can. The same applies to most of the "ooh,
macros are for *this*!" revelations that people have early on. I
started out thinking that they were just for writing functions that
didn't evaluate their arguments until they were ready to. And they
can, indeed, do just that. My point is this: don't limit your
understanding of macros to the first thing you find that they can do.
.
- Follow-Ups:
- Re: eval in bash vs macro in lisp
- From: Duane Rettig
- Re: eval in bash vs macro in lisp
- From: Weiguang Shi
- Re: eval in bash vs macro in lisp
- References:
- eval in bash vs macro in lisp
- From: Weiguang Shi
- Re: eval in bash vs macro in lisp
- From: Pascal Bourguignon
- Re: eval in bash vs macro in lisp
- From: Weiguang Shi
- eval in bash vs macro in lisp
- Prev by Date: Firefox sidebar for Lisp Reference
- Next by Date: Re: ANN: Graphic-Forms version 0.5.0 released
- Previous by thread: Re: eval in bash vs macro in lisp
- Next by thread: Re: eval in bash vs macro in lisp
- Index(es):
Relevant Pages
|