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

if x in (list 1 2 3)

0 views
Skip to first unread message

Thomas Guettler

unread,
Sep 25, 2002, 6:45:27 AM9/25/02
to
Hi!

Is ther an "in" operator which does the following:

(if x in (list 1 2 3))

This should return true if x is 1, 2 or 3.

thomas


Marco Baringer

unread,
Sep 25, 2002, 6:49:40 AM9/25/02
to
Thomas Guettler <zopes...@thomas-guettler.de> writes:

see the function MEMBER. FIND might be interesting as well.

--
-Marco
Ring the bells that still can ring.
Forget your perfect offering.
There is a crack in everything.
That's how the light gets in.
-Leonard Cohen

Christian Nybų

unread,
Sep 25, 2002, 8:07:42 AM9/25/02
to
Marco Baringer <em...@bese.it> writes:

> Thomas Guettler <zopes...@thomas-guettler.de> writes:
>
> > Hi!
> >

> > Is there an "in" operator which does the following:


> >
> > (if x in (list 1 2 3))
> >
> > This should return true if x is 1, 2 or 3.
> >
> > thomas
>
> see the function MEMBER. FIND might be interesting as well.

As well as CASE, I think.
--
chr

Kaz Kylheku

unread,
Sep 25, 2002, 12:04:35 PM9/25/02
to
Thomas Guettler <zopes...@thomas-guettler.de> wrote in message news:<ams3vk$s52$00$1...@news.t-online.com>...

The functions FIND and FIND-IF search through sequences (vectors or
lists). It takes some interesting keyword parameters so that you can
do a fine-tuned search, for instance:

(find "smith" '(("smith" 33) ("brook" 22)) :key #'first :test
#'equalp)
==> ("smith" 33)

(find-if (lambda (x) (> (first x) (second x))) '((1 2) (5 4) (3 4)))
==> (5 4)

The return value is a generalized boolean, so you can use FIND or
FIND-IF for membership testing.

There is also a function called MEMBER which is less general; it works
for lists only.

Lastly, there are some set predicate functions you might be interested
in; these are generalized to vectors and lists:

;; is there some element that is equal to 2?
(some (lambda (element) (= element 1)) '(1 2 3))
==> T

;; are all elements odd?
(every #'oddp '(1 2 5))
==> NIL

;; how about now, and using a vector?
(every #'oddp #(1 3 5))
===> T

The MEMBER function is specific to lists, and is useful because it
gives you the cons cell where the match is found, rather than
navigating to the matching value directly (which is the CAR of that
cell).

(member 2 '(1 2 3))
==> (2 3)

Hope this helps.

Michael A. Koerber

unread,
Sep 25, 2002, 2:19:09 PM9/25/02
to
Try
(let ((x 3))
(if (member x (list 1 2 3)) 'yes 'no))

Erik Naggum

unread,
Sep 25, 2002, 5:46:11 PM9/25/02
to
* Thomas Guettler

| Is ther an "in" operator which does the following:
|
| (if x in (list 1 2 3))
|
| This should return true if x is 1, 2 or 3.

The function you want is one of `member´, `find´, or even `position´.

If you want more syntactically convenient support, consider `case´ and its
friends `ccase´ and `ecase´:

(case x
((1 2 3) ...)
(t ...))

--
Erik Naggum, Oslo, Norway

Act from reason, and failure makes you rethink and study harder.
Act from faith, and failure makes you blame someone and push harder.

Coby Beck

unread,
Sep 25, 2002, 9:28:58 PM9/25/02
to

"Michael A. Koerber" <m...@ll.mit.edu> wrote in message
news:3D91FE1...@ll.mit.edu...

> Try
> (let ((x 3))
> (if (member x (list 1 2 3)) 'yes 'no))

But in Lisp 'yes is t and 'no is nil. Most predicates will return nil or
anything else for 'no and 'yes.

(let ((x 3))
(member x (list 1 2 3)))

--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")


0 new messages