Re: Appropriate case for a macro?
- From: Ken Tilton <kennytilton@xxxxxxxxxxxxx>
- Date: Mon, 31 Mar 2008 13:52:12 -0400
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.
(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
.
- Follow-Ups:
- Re: Appropriate case for a macro?
- From: Brian Adkins
- Re: Appropriate case for a macro?
- References:
- Appropriate case for a macro?
- From: Brian Adkins
- Re: Appropriate case for a macro?
- From: Chris Riesbeck
- Appropriate case for a macro?
- Prev by Date: Re: Appropriate case for a macro?
- Next by Date: Re: (apply #'and '(t t nil)) not work
- Previous by thread: Re: Appropriate case for a macro?
- Next by thread: Re: Appropriate case for a macro?
- Index(es):
Relevant Pages
|