Re: eval in bash vs macro in lisp



In article <m28xlccaz8.fsf@xxxxxxxxxxxxxxxxx>, Ari Johnson wrote:
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.
I understand what the word means. I meant it and don't think that's
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 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.
It's a good point and thanks.

Wei
.



Relevant Pages

  • Re: Very poor Lisp performance
    ... >>> Do you mean it is difficult to implement infix in Lisp? ... > to write macros that use infix syntax? ... your language is infix it's harder to write macros because the macros ...
    (comp.lang.lisp)
  • Re: Two questions together
    ... How powerful are the macros? ... > "programmable language" and as far as I know this feature is provided ... It is also said that one can hammer the Lisp so that ... I really love Lisp's syntax:) But I just want ...
    (comp.lang.lisp)
  • Re: macro for shorter array syntax
    ... This implies that OCaml macros can be easier which, in turn, justifies my ... Lisp manuals and tutorials say macros define new syntax, ...
    (comp.lang.lisp)
  • Re: Benefits of Dynamic Typing
    ... Many dynamically typed languages (e.g. Python, ... > So I think the assertion is only true if it is changed to "Lisp's syntax is ... > is "Lisp was designed to have a simple syntax". ... So there is syntax expressed via macros or built-in special forms. ...
    (comp.lang.functional)
  • Re: Cons cell archaic!?
    ... How would the implementation of a Lisp without a using cons look like? ... the irregularity in its often cited regular syntax. ... Lisp at core is based on functional programing on lists. ...
    (comp.lang.lisp)