Re: eval in bash vs macro in lisp



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.
.



Relevant Pages

  • Re: New book about Common Lisp: Let Over Lambda
    ... I have read every lisp book I've been able to get my hands on ... Lisp macros are not given nearly enough ... I personally consider Let Over Lambda to be an unofficial sequel ... integral parts of the language. ...
    (comp.lang.lisp)
  • Re: LISPPA
    ... > which describes the unification algorithm in Scheme. ... But let's get back to why I think lisp is superior ... optimization macros or reader ... cases where a running program cannot continue running, ...
    (comp.lang.lisp)
  • Re: Macro functionality at runtime?
    ... we don't know what the subtree is yet. ... "Returns a list of tokens matched to subtree. ... > be optimized out by the lisp compiler. ... >> If we have first class macros, ...
    (comp.lang.lisp)
  • Re: What is the main advantage of macros over functions?
    ... if one is allergic to reading Lambda one may better not use ... Lisp authors, doesn't help you much, I'd say. ... >> Readmacros are a completely different mechanism. ... Macros and Readmacros ...
    (comp.lang.lisp)
  • Re: Python syntax in Lisp and Scheme
    ... Alex Martelli wrote: ... > I never used Common Lisp in production: in the period of my life when I ... > did not happen to use it in TI), with some of the divergence hinging on ... > locally developed sets of macros. ...
    (comp.lang.lisp)