"member" function

362 views
Skip to first unread message

David Sletten

unread,
Mar 2, 2009, 7:54:56 AM3/2/09
to clo...@googlegroups.com
Does Clojure have an analog of Lisp's MEMBER function?
(member 'a '(c a f e b a b e)) => (A F E B A B E)

(I'm more interested in it's use as a predicate rather than the fact
that it returns a sublist when true.)

"find" and "contains?" are listed under the Maps section of the data
structures page (http://clojure.org/data_structures#toc17) so no good
with lists. I can't think of any other synonyms.

The documentation for "some" suggests this:
(some (fn [elt] (= elt 'a)) '(a b c)) => true
That's pretty verbose, and even the shortcut is kinda long:
(some #(= % 'a) '(a b c)) => true

Although this alternative is alright:
(some #{'a} '(a b c)) => a

Is that the Clojure version of MEMBER?

Aloha,
David Sletten

Meikel Brandmeyer

unread,
Mar 2, 2009, 8:39:30 AM3/2/09
to clo...@googlegroups.com
Hi David,

Am 02.03.2009 um 13:54 schrieb David Sletten:

> Does Clojure have an analog of Lisp's MEMBER function?
> (member 'a '(c a f e b a b e)) => (A F E B A B E)

I don't know the member function of CL, but I interpret
your example, that it cuts away the head of the list until
the first occurence of the given thing.

You can do that in Clojure with drop-while:

(drop-while #(not= % :a) (list :c :a :f :e :b :a :b :e))
=> (:a :f :e :b :a :b :e)

Hope this helps.

Sincerely
Meikel

David Sletten

unread,
Mar 2, 2009, 8:51:41 AM3/2/09
to clo...@googlegroups.com

On Mar 2, 2009, at 3:39 AM, Meikel Brandmeyer wrote:
>
>> Does Clojure have an analog of Lisp's MEMBER function?
>> (member 'a '(c a f e b a b e)) => (A F E B A B E)
>
> I don't know the member function of CL, but I interpret
> your example, that it cuts away the head of the list until
> the first occurence of the given thing.
>

MEMBER is a fancy predicate that tests whether or not an item is an
element of a given list. I say fancy because rather than simply
returning true when the item is present it returns the tail of the
list starting with the item. It returns nil otherwise. I don't care
about this fancy value. I just want to know whether or not the item
is in the list without too much verbosity.

I think (some #{<ITEM>} <LIST>) is probably the way to do it.

> You can do that in Clojure with drop-while:
>
> (drop-while #(not= % :a) (list :c :a :f :e :b :a :b :e))
> => (:a :f :e :b :a :b :e)
>

Thanks for your example anyway.

Mahalo nui loa (vielen dank),
David Sletten

Mark Volkmann

unread,
Mar 2, 2009, 9:01:24 AM3/2/09
to clo...@googlegroups.com
On Mon, Mar 2, 2009 at 6:54 AM, David Sletten <da...@bosatsu.net> wrote:
>
> Does Clojure have an analog of Lisp's MEMBER function?
> (member 'a '(c a f e b a b e)) => (A F E B A B E)
>
> (I'm more interested in it's use as a predicate rather than the fact
> that it returns a sublist when true.)
>
> "find" and "contains?" are listed under the Maps section of the data
> structures page (http://clojure.org/data_structures#toc17) so no good
> with lists. I can't think of any other synonyms.
>
> The documentation for "some" suggests this:
> (some (fn [elt] (= elt 'a)) '(a b c)) => true
> That's pretty verbose, and even the shortcut is kinda long:
> (some #(= % 'a) '(a b c)) => true

It's verbose in order to discourage its use since its a linear search.
See the discussion about the contains? function at
http://www.ociweb.com/mark/clojure/article.html#Lists
and http://www.ociweb.com/mark/clojure/article.html#Sets.

--
R. Mark Volkmann
Object Computing, Inc.

David Sletten

unread,
Mar 2, 2009, 9:07:35 AM3/2/09
to clo...@googlegroups.com

On Mar 2, 2009, at 4:01 AM, Mark Volkmann wrote:

>
> It's verbose in order to discourage its use since its a linear search.
> See the discussion about the contains? function at
> http://www.ociweb.com/mark/clojure/article.html#Lists
> and http://www.ociweb.com/mark/clojure/article.html#Sets.

Ahh. Nice explanation. Thanks again Mark for the article.

<sheepish>
As you can see I haven't finished reading the whole thing yet.
</sheepish>

Aloha,
David Sletten


Jason Wolfe

unread,
Mar 2, 2009, 5:51:16 PM3/2/09
to Clojure
There's also "includes?" in clojure.contrib.seq-utils.

-Jason

On Mar 2, 6:07 am, David Sletten <da...@bosatsu.net> wrote:
> On Mar 2, 2009, at 4:01 AM, Mark Volkmann wrote:
>
>
>
> > It's verbose in order to discourage its use since its a linear search.
> > See the discussion about the contains? function at
> >http://www.ociweb.com/mark/clojure/article.html#Lists
> > andhttp://www.ociweb.com/mark/clojure/article.html#Sets.
Reply all
Reply to author
Forward
0 new messages