Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

cond-let-p

13 views
Skip to first unread message

Manuel Giraud

unread,
Feb 14, 2002, 5:16:11 AM2/14/02
to

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.

--
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_

Dr. Edmund Weitz

unread,
Feb 14, 2002, 6:34:56 AM2/14/02
to
Manuel Giraud <gir...@sor.inria.fr> writes:

> 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/>

Erik Naggum

unread,
Feb 14, 2002, 8:58:51 AM2/14/02
to
* Manuel Giraud <gir...@sor.inria.fr>

| 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.

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.

Michael Parker

unread,
Feb 14, 2002, 10:57:06 AM2/14/02
to
Manuel Giraud <gir...@sor.inria.fr> wrote in message news:<qs38z9w...@yoko.inria.fr>...

> 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.

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.

Wolfhard Buß

unread,
Feb 14, 2002, 12:55:23 PM2/14/02
to
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 ;)
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.

Thomas F. Burdick

unread,
Feb 14, 2002, 2:41:03 PM2/14/02
to
wb...@gmx.net (Wolfhard Buß) writes:

> 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! |
/ / `-----------------------'
( -. |
| ) |
(`-. '--.)
`. )----'

Larry Clapp

unread,
Feb 14, 2002, 7:48:53 PM2/14/02
to
In article <qs38z9w...@yoko.inria.fr>, Manuel Giraud wrote:
> 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.

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. :)

Pekka P. Pirinen

unread,
Feb 20, 2002, 2:20:01 PM2/20/02
to
Larry Clapp <lam...@theclapp.org> writes:
> In article <qs38z9w...@yoko.inria.fr>, Manuel Giraud wrote:
> > Is there in CL a form that let you do stuff like that :
> >
> > (cond-let ((a (func)) (* a a))
> > (t (error "meuh")))
>
> Paul Graham defines a condlet macro in On Lisp. See page 146. On Lisp is
> available online at www.paulgraham.com.

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

Francis Leboutte

unread,
Feb 21, 2002, 6:54:15 AM2/21/02
to
Pekka.P...@globalgraphics.com (Pekka P. Pirinen) wrote:


>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

0 new messages