Re: Appropriate case for a macro?



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)
(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

.



Relevant Pages

  • Re: Appropriate case for a macro?
    ... One of the things that attracts me to Lisp is the macro ... This can be made more concise via an assignment: ... (bar lhs)) ...
    (comp.lang.lisp)
  • Re: Appropriate case for a macro?
    ... One of the things that attracts me to Lisp is the macro ... Consider the following pattern: ... (bar lhs)) ...
    (comp.lang.lisp)
  • Appropriate case for a macro?
    ... One of the things that attracts me to Lisp is the macro ... This can be made more concise via an assignment: ... (bar lhs)) ...
    (comp.lang.lisp)
  • Re: Appropriate case for a macro?
    ... One of the things that attracts me to Lisp is the macro ... (bar lhs)) ... where a-long-expression has been replaced by lhs ...
    (comp.lang.lisp)
  • 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)