I want to test whether a list (set) has an item. Is there any build-in
function like containp?
If not then I have to write a loop and scan every item which is quite
inefficient.
Or, is there any functions for quick sort or hash table something?
From: Jens Teich
Subject: Re: How to check whether a list has an item?
Date:
Message-ID: <m2prmgezmj.fsf@jensteich.de>
smartnose <·········@gmail.com> writes:
> I want to test whether a list (set) has an item. Is there any build-in
> function like containp?
CL-USER> (member 42 (list 1 2 3))
NIL
CL-USER> (member 2 (list 1 2 3))
(2 3)
Jens
smartnose <·········@gmail.com> writes:
> I want to test whether a list (set) has an item. Is there any build-in
> function like containp?
>
> If not then I have to write a loop and scan every item which is quite
> inefficient.
As mentionned, MEMBER or FIND (sometimes I use POSITION too) can
indicate whether a sequence contains an item. But you should also
consider what it means. By default, CL function will test for EQL
items. But for most items, this is too strong a match.
For example, if you want to find a string in a list, you may want to
use EQUAL (case sensitive), EQUALP (case insensitive), STRING= or
STRING-EQUALP (for string designers), or even some other custom
equality:
(member "bitsy" '("itsy" "bitsy" "tiny" "weenie")) --> NIL
(member "bitsy" '("itsy" "bitsy" "tiny" "weenie") :test (function equal)) --> true
(member "BITSY" '("itsy" "bitsy" "tiny" "weenie") :test (function equal)) --> NIL
(member "BITSY" '("itsy" "bitsy" "tiny" "weenie") :test (function equalp)) --> true
(member "BITSY" '(itsy bitsy tiny weenie) :test (function equalp)) --> NIL
(member "BITSY" '(itsy bitsy tiny weenie) :test (function string=)) --> true
(member "bitsy" '(itsy bitsy tiny weenie) :test (function string=)) --> NIL
(member "bitsy" '(itsy bitsy tiny weenie) :test (function string-equal)) --> true
> Or, is there any functions for quick sort or hash table something?
Yes, there exist such functions too. What do you want to do?
--
__Pascal Bourguignon__
On Oct 4, 3:48 pm, smartnose <·········@gmail.com> wrote:
> I want to test whether a list (set) has an item. Is there any build-in
> function like containp?
>
> If not then I have to write a loop and scan every item which is quite
> inefficient.
The other answers are sufficient, but this bit about efficiency
bothers me. How do you expect a built in function to operate if not by
scanning every item and testing for some sort of equivalence?
Cheers,
drewc
>
> Or, is there any functions for quick sort or hash table something?