Re: eval in bash vs macro in lisp
- From: Weiguang Shi <wgshi@xxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 26 Aug 2006 08:05:25 +0000 (UTC)
In article <m28xlccaz8.fsf@xxxxxxxxxxxxxxxxx>, Ari Johnson wrote:
Weiguang Shi <wgshi@xxxxxxxxxxxxxxxxxxxxx> writes:I understand what the word means. I meant it and don't think that's
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.
impossible. But that's off-topic.
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.
One can certainly create a function with a name and parameter
sequence, call it ``new'' syntax, and inside the function evaluate
them into a sequence meaningful in the original bash syntax. After
all, they (the ``new'' syntaxes) are just functions, either in lisp or
bash, are they not? I completely fail to see how macro excels here.
Just because macros can do one thing that you can do with eval inIt's a good point and thanks.
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.
Wei
.
- Follow-Ups:
- Re: eval in bash vs macro in lisp
- From: Kaz Kylheku
- Re: eval in bash vs macro in lisp
- From: Ari Johnson
- 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
- Re: eval in bash vs macro in lisp
- From: Ari Johnson
- eval in bash vs macro in lisp
- Prev by Date: Re: string to macro
- Next by Date: floating point calculations, what does equal mean?
- 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
|