Re: Appropriate case for a macro?



On Mar 31, 1:52 pm, Ken Tilton <kennytil...@xxxxxxxxxxxxx> wrote:
Chris Riesbeck wrote:
Brian Adkins wrote:

I program primarily in Ruby and have been trying to find time to learn
Lisp. One of the things that attracts me to Lisp is the macro
facility. Consider the following pattern:

if a_long_expression > value
foo(a_long_expression)
bar(a_long_expression)
else
baz(a_long_expression)
end

This can be made more concise via an assignment:

if (x = a_long_expression) > value
foo(x)
bar(x)
else
baz(x)
end

However, this seems like a pragmatic hack since a new variable is
introduced simply for conciseness. Would you consider this an
appropriate case for a macro in Common Lisp?

Certainly if you're going to do it, you'd do it with macros. Whether you
should do it is a matter of debate.

Perhaps used as follows:

(my-if (> a-long-expression value)

Not such a great example since > will return t or nil, but we get the idea.

Well, I did want > to return t or nil so that either the 'then' or
'else' expression would be evaluated - I also wanted to grab the left
hand side to be used in the body.

But I agree that it's a poor example after reflecting on it a bit. I
noticed a repetitive pattern in Ruby code and had a knee-jerk reaction
about how I might reduce that if Ruby had macros, but either way a
variable will be introduced, so the explicit in place assignment is
hard to improve on in the original i.e.

changing: if a-long-expression > value
to: if (x = a-long-expression) > value

doesn't seem so bad, and makes clear what is happening. I'll have to
look elsewhere for compelling macro advantages. I may find them more
readily by coding more Lisp than by looking for opportunities in a
language that doesn't provide them. Of course, just converting the
some of the Rails runtime metaprogramming to compile time is pretty
compelling.

(progn
(foo lhs)
(bar lhs))
(baz lhs))

where a-long-expression has been replaced by lhs

See Anaphoric Macros for a common approach to this:

http://www.bookshelf.jp/texi/onlisp/onlisp_17.html#SEC111

And throw in a var to be found so you can nest the things without surprises:

(my-if ale (a-long-search expression value)
(drink ale)
(go-thirsty))

My bif has a little diff syntax:

(bif (ale (a-long.....)) ; extra pair of parens
etc etc)

kt

--http://smuglispweeny.blogspot.com/http://www.theoryyalgebra.com/

"In the morning, hear the Way;
in the evening, die content!"
-- Confucius

.



Relevant Pages

  • Re: Three questions
    ... of the functionality you are doing here already exists in lisp. ... Another way to implement this macro, ... really understand all of the binding forms in Common Lisp and also make ... often KEY for properly implementing control structures. ...
    (comp.lang.lisp)
  • [OT] PostLisp, a language experiment
    ... IMHO the worst thing in Lisp is its many parentheses. ... The worst thing about Forth IMHO is the stack clutter. ... And while I could write a macro like (with bla ... The ret is the explicit return continuation. ...
    (comp.lang.lisp)
  • Re: Program compression
    ... problems to be solved much more concisely than with Lisp. ... In Common Lisp, it's another one line of code: ... My point is that different languages are based on different things ... it would be impossible for the author of a macro (in the Lisp ...
    (comp.programming)
  • Re: Why is LISP syntax superior?
    ... that lisp was the most powerful programming language ever invented, ... It is precisely the lack of syntax that I admire in Lisp ... (setf (elt seq i) ... I guess one could write a reader macro to transform foointo (elt ...
    (comp.lang.lisp)
  • Re: Python gets macros - now XML does too
    ... > I think the main advantage: You use Lisp within a Lisp macro to generate ... > generation part in another language than the description to generate the ... MetaL compiler flow module provides support for level 0 (macro ... Despite currently I only use PHP because I only develop Web ...
    (comp.lang.lisp)