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