(cond-let ((a (func)) (* a a))
(t (error "meuh")))
Where a clause is accepted if (func) returns a non-nil value
and then 'a' is bound to this value in the remaining
consequent.
--
Jake:"See those berries? That's our breakfast! See that stream? That's
our drinking water!"
Daria:"See that skeleton? That's our future."
-From "The Teachings Of Don Jake."
_Manuel Giraud_
> Hi,
> Is there in CL a form that let you do stuff like that :
>
> (cond-let ((a (func)) (* a a))
> (t (error "meuh")))
>
> Where a clause is accepted if (func) returns a non-nil value
> and then 'a' is bound to this value in the remaining
> consequent.
Maybe something like that:
(defmacro cond-let (&rest clauses)
(if (null clauses)
nil
(let* ((first-clause (car clauses))
(let-form (car first-clause))
(var (car let-form)))
(if (eq let-form t)
`(progn ,@(cdr first-clause))
`(let (,let-form)
(if ,var
,@(cdr first-clause)
(cond-let ,@(cdr clauses))))))))
Edi.
--
Dr. Edmund Weitz
Hamburg
Germany
The Common Lisp Cookbook
<http://agharta.de/cookbook/>
You are probably aware of the ugly Scheme hack with -> to pass the result
of the expression to a function in the body. I toyed with a funcond
(hybrid of funcall and cond) once that went like this:
(funcond ((func) (lambda (x) (* x x)))
(t (lambda (x) (declare (ignore x)) (error "meuh"))))
For fairly obvious reasons, I dropped this, having learned something
about language design.
The not too infrequently used way to do this, however, goes like this:
(let (x)
(cond ((setq x (func))
(* x x))
(t (error "meuh"))))
Which is probably a macro should do if you do not like to see it directly
in your own code.
///
--
In a fight against something, the fight has value, victory has none.
In a fight for something, the fight is a loss, victory merely relief.
Not built-in. The cond in scheme does this, although with a different
syntax. I've got a CL version of the scheme cond on my webpage at
http://www.geocities.com/mparker762. It's called bcond (for binding
cond),
accepts basically the scheme syntax, and goes to some effort to be
efficient
about it all.
> Is there in CL a form that let you do stuff like that :
>
> (cond-let ((a (func)) (* a a))
> (t (error "meuh")))
>
> Where a clause is accepted if (func) returns a non-nil value
> and then 'a' is bound to this value in the remaining
> consequent.
I don't like cond-let ;)
But there is Paul Graham's anaphoric version of cond
(defmacro acond (&rest clauses)
(if (null clauses)
nil
(let ((cl1 (car clauses))
(sym (gensym)))
`(let ((,sym ,(car cl1)))
(if ,sym
(let ((it ,sym)) ,@(cdr cl1))
(acond ,@(cdr clauses)))))))
which binds the 'free' variable it to the value of
the condition.
Example:
(acond ((1+ 20) (+ it it))
(t (error "meuh"))))
=> 42
--
Some Lispers don't like anaphorisms.
> Manuel Giraud <gir...@sor.inria.fr> writes:
>
> > Is there in CL a form that let you do stuff like that :
> >
> > (cond-let ((a (func)) (* a a))
> > (t (error "meuh")))
> >
> > Where a clause is accepted if (func) returns a non-nil value
> > and then 'a' is bound to this value in the remaining
> > consequent.
>
> I don't like cond-let ;)
Me neither, but I like it more than Graham's weird stealing of IT.
> But there is Paul Graham's anaphoric version of cond
>
> (defmacro acond (&rest clauses)
> (if (null clauses)
> nil
> (let ((cl1 (car clauses))
> (sym (gensym)))
> `(let ((,sym ,(car cl1)))
> (if ,sym
> (let ((it ,sym)) ,@(cdr cl1))
> (acond ,@(cdr clauses)))))))
>
> which binds the 'free' variable it to the value of
> the condition.
>
> Example:
> (acond ((1+ 20) (+ it it))
> (t (error "meuh"))))
> => 42
I prefer to use:
(defmacro binding-cond (var &rest clauses)
`(let (,var)
(cond ,(loop for (pred . body) in clauses
if (eq pred t) collect (cons pred body)
else collect `((setf ,var ,pred) ,@body)))))
Example:
(binding-cond this
((member x foo) foo)
((member y foo) (binding-cond that
((member x bar) bar)
((member y bar) (frob this that))
(t y)))
(t x))
--
/|_ .-----------------------.
,' .\ / | No to Imperialist war |
,--' _,' | Wage class war! |
/ / `-----------------------'
( -. |
| ) |
(`-. '--.)
`. )----'
Paul Graham defines a condlet macro in On Lisp. See page 146. On Lisp is
available online at www.paulgraham.com.
-- Larry Clapp, who's gotten up to p. 147 in On Lisp. :)
Everybody and their dog seems to be doing this. LispWorks has an
extension called WHEN-LET
<http://www.xanalys.com/software_tools/reference/lwl42/LWRM-U/html/lwref-u-257.htm#pgfId-889998>
that is like WHEN rather than COND:
(when-let (position (search string1 string2))
(print position))
We haven't often missed an else-branch, but perhaps that's one of
those things that you don't miss, but you end up using a lot if it's
available.
--
Pekka P. Pirinen
"A man never speaks of himself without losing something. What he says
in his disfavor is always believed but when he commends himself, he arouses
mistrust." - Michel Montaigne
>Everybody and their dog seems to be doing this. LispWorks has an
>extension called WHEN-LET
><http://www.xanalys.com/software_tools/reference/lwl42/LWRM-U/html/lwref-u-257.htm#pgfId-889998>
>that is like WHEN rather than COND:
> (when-let (position (search string1 string2))
> (print position))
my dog is lazy, he is doing:
>U(9): (when-bind (v -1 #'oddp #'minusp)
T)
t
when-bind var form &rest predicates
--
www.algo.be
Logo programming : www.algo.be/logo.html