From: iu2
Subject: find an element in a list
Date: 
Message-ID: <1190207136.188955.84540@19g2000hsx.googlegroups.com>
Hi all,

Hi all,
Is there an essential differenct between assoc, member and find?

CL-USER> (setf lis1 '((A . 1) (B . 2) (C . 3)))
((A . 1) (B . 2) (C . 3))
CL-USER> (assoc 'b lis1)
(B . 2)

CL-USER> (setf lis2 '((A 1) (B 2) (C 3)))
((A 1) (B 2) (C 3))
CL-USER> (member 'C lis2 :key #'car)
((C 3))
CL-USER> (find 'b lis2 :key #'car)
(B 2)

thanks
iu2

From: John Thingstad
Subject: Re: find an element in a list
Date: 
Message-ID: <op.tywkz1eypqzri1@pandora.upc.no>
P� Wed, 19 Sep 2007 15:05:36 +0200, skrev iu2 <·······@elbit.co.il>:

> Hi all,
>
> Hi all,
> Is there an essential differenct between assoc, member and find?
>
> CL-USER> (setf lis1 '((A . 1) (B . 2) (C . 3)))
> ((A . 1) (B . 2) (C . 3))
> CL-USER> (assoc 'b lis1)
> (B . 2)
>
> CL-USER> (setf lis2 '((A 1) (B 2) (C 3)))
> ((A 1) (B 2) (C 3))
> CL-USER> (member 'C lis2 :key #'car)
> ((C 3))
> CL-USER> (find 'b lis2 :key #'car)
> (B 2)
>
> thanks
> iu2
>


You seem to have found it! :) No they are not fundemetally different. It  
depeds on the use.
For details look at the hyperspec. (but you have already done that)
Welcome to the Lisp club..
From: Alan Crowe
Subject: Re: find an element in a list
Date: 
Message-ID: <86ps0eiqzd.fsf@cawtech.freeserve.co.uk>
iu2 <·······@elbit.co.il> writes:

> Hi all,
> 
> Hi all,
> Is there an essential differenct between assoc, member and find?
> 
> CL-USER> (setf lis1 '((A . 1) (B . 2) (C . 3)))
> ((A . 1) (B . 2) (C . 3))
> CL-USER> (assoc 'b lis1)
> (B . 2)
> 
> CL-USER> (setf lis2 '((A 1) (B 2) (C 3)))
> ((A 1) (B 2) (C 3))
> CL-USER> (member 'C lis2 :key #'car)
> ((C 3))
> CL-USER> (find 'b lis2 :key #'car)
> (B 2)
> 

> thanks
> iu2

There is a curious twist at the bottom of the page of the
hyperspec for assoc

    The two expressions 

     (assoc item list :test fn)

    and 

     (find item list :test fn :key #'car)

    are equivalent in meaning with one exception: if nil
    appears in alist in place of a pair, and item is nil,
    find will compute the car of the nil in alist, find that
    it is equal to item, and return nil, whereas assoc will
    ignore the nil in alist and continue to search for an
    actual cons whose car is nil.

Why are nil's are allowed in association lists?

I have come across one case in which this is
convenient. Think about pattern matching code. 

(a b):(1 2) => ((a . 1)(b . 2))

(a a):(1 2) => nil

(a 2):(1 2) => ((a . 1))

(1 2):(1 2) => ????

We have a problem. We would like a match with no variables
to either succeed returning an association list (which is going
to be empty) or fail, returning nil, and we would like these
to be distinct. We can get round this by using '(nil) as our
initial empty list of bindings. It is a cons, hence not
null, but (assoc ...) => nil, guaranteed.

However I'm not much impressed. I've coded up a pattern
matcher that returns :fail when it fails, instead of nil. So
I have to write

(if (eql result :fail)
    :fail
    (do-more-matching other-half-of-match result)

instead of 

(and answer (do-more-matching other-half-of-match result))

Using an explicit failure symbol gave code that was slightly
long, but which read well.

Alan Crowe
Edinburgh
Scotland

 
From: Kyle McGivney
Subject: Re: find an element in a list
Date: 
Message-ID: <1190232048.380370.237300@w3g2000hsg.googlegroups.com>
On Sep 19, 9:05 am, iu2 <·······@elbit.co.il> wrote:
> Hi all,
>
> Hi all,
> Is there an essential differenct between assoc, member and find?
>
> CL-USER> (setf lis1 '((A . 1) (B . 2) (C . 3)))
> ((A . 1) (B . 2) (C . 3))
> CL-USER> (assoc 'b lis1)
> (B . 2)
>
> CL-USER> (setf lis2 '((A 1) (B 2) (C 3)))
> ((A 1) (B 2) (C 3))
> CL-USER> (member 'C lis2 :key #'car)
> ((C 3))
> CL-USER> (find 'b lis2 :key #'car)
> (B 2)
>
> thanks
> iu2

Just wondering: do you know about the hyperspec? Do you somehow find
it better to post here and ask than just read the answer over there?
It can't be faster... perhaps it's more entertaining?

Kyle
From: Pillsy
Subject: Re: find an element in a list
Date: 
Message-ID: <1190233454.044175.9530@d55g2000hsg.googlegroups.com>
On Sep 19, 4:00 pm, Kyle McGivney <·······@gmail.com> wrote:
[...]
> Just wondering: do you know about the hyperspec? Do you somehow find
> it better to post here and ask than just read the answer over there?
> It can't be faster... perhaps it's more entertaining?

I remember that back in my newbie days, the HyperSpec was something it
took me a little while to get used to.

It's just a description of the language. It doesn't provide a lot of
help if you want to know which Common Lisp function you should use for
a given purpose, and it's not going to satisfy your curiosity if
you're wondering why CL has five functions that do such similar
things.

Cheers,
Pillsy
From: Barry Margolin
Subject: Re: find an element in a list
Date: 
Message-ID: <barmar-0F4097.23504919092007@comcast.dca.giganews.com>
In article <······················@d55g2000hsg.googlegroups.com>,
 Pillsy <·········@gmail.com> wrote:

> On Sep 19, 4:00 pm, Kyle McGivney <·······@gmail.com> wrote:
> [...]
> > Just wondering: do you know about the hyperspec? Do you somehow find
> > it better to post here and ask than just read the answer over there?
> > It can't be faster... perhaps it's more entertaining?
> 
> I remember that back in my newbie days, the HyperSpec was something it
> took me a little while to get used to.
> 
> It's just a description of the language. It doesn't provide a lot of
> help if you want to know which Common Lisp function you should use for
> a given purpose, and it's not going to satisfy your curiosity if
> you're wondering why CL has five functions that do such similar
> things.

Practically every time you're wondering why CL has many similar 
functions, the answer is simple: history.  I.e. it's for compatibility 
with earlier Lisp dialects.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: iu2
Subject: Re: find an element in a list
Date: 
Message-ID: <1190271917.672556.245850@d55g2000hsg.googlegroups.com>
On Sep 19, 10:00 pm, Kyle McGivney <·······@gmail.com> wrote:
> On Sep 19, 9:05 am, iu2 <·······@elbit.co.il> wrote:
>
>
>
>
>
> > Hi all,
>
> > Hi all,
> > Is there an essential differenct between assoc, member and find?
>
> > CL-USER> (setf lis1 '((A . 1) (B . 2) (C . 3)))
> > ((A . 1) (B . 2) (C . 3))
> > CL-USER> (assoc 'b lis1)
> > (B . 2)
>
> > CL-USER> (setf lis2 '((A 1) (B 2) (C 3)))
> > ((A 1) (B 2) (C 3))
> > CL-USER> (member 'C lis2 :key #'car)
> > ((C 3))
> > CL-USER> (find 'b lis2 :key #'car)
> > (B 2)
>
> > thanks
> > iu2
>
> Just wondering: do you know about the hyperspec? Do you somehow find
> it better to post here and ask than just read the answer over there?
> It can't be faster... perhaps it's more entertaining?
>
> Kyle- Hide quoted text -
>
> - Show quoted text -

No, I wans't aware of it. No, I'm not posting here for entertaining,
and I'm sorry if it offended someone.
I post questions in the comp.lang,tcl, C++ and lisp forums, and I get
a lot of help from them.
I believe the hackers answering the questions feel an urge to do that,
they like to share what they know. I feel like this myself sometimes
(although a newbie). So I post when I have a question. I always find
it amazing how fast the answers arrive. Sometimes in an hour or so.
I did receive one or two cynical responses once, and I couldn't figure
out why. Apparently some have a solid idea of what questions and
comments should be posted, and which should not.
I'm used to say to colleagues at work that there are no silly
questions. There are only silly answers.
From: Kyle McGivney
Subject: Re: find an element in a list
Date: 
Message-ID: <1190395234.912109.10260@19g2000hsx.googlegroups.com>
On Sep 20, 3:05 am, iu2 <·······@elbit.co.il> wrote:
> On Sep 19, 10:00 pm, Kyle McGivney <·······@gmail.com> wrote:
>
>
>
>
>
> > On Sep 19, 9:05 am, iu2 <·······@elbit.co.il> wrote:
>
> > > Hi all,
>
> > > Hi all,
> > > Is there an essential differenct between assoc, member and find?
>
> > > CL-USER> (setf lis1 '((A . 1) (B . 2) (C . 3)))
> > > ((A . 1) (B . 2) (C . 3))
> > > CL-USER> (assoc 'b lis1)
> > > (B . 2)
>
> > > CL-USER> (setf lis2 '((A 1) (B 2) (C 3)))
> > > ((A 1) (B 2) (C 3))
> > > CL-USER> (member 'C lis2 :key #'car)
> > > ((C 3))
> > > CL-USER> (find 'b lis2 :key #'car)
> > > (B 2)
>
> > > thanks
> > > iu2
>
> > Just wondering: do you know about the hyperspec? Do you somehow find
> > it better to post here and ask than just read the answer over there?
> > It can't be faster... perhaps it's more entertaining?
>
> > Kyle- Hide quoted text -
>
> > - Show quoted text -
>
> No, I wans't aware of it. No, I'm not posting here for entertaining,
> and I'm sorry if it offended someone.

I don't see that anyone has posted a link yet in this thread:

http://www.lisp.org/HyperSpec/FrontMatter/index.html

> I post questions in the comp.lang,tcl, C++ and lisp forums, and I get
> a lot of help from them.
> I believe the hackers answering the questions feel an urge to do that,
> they like to share what they know. I feel like this myself sometimes
> (although a newbie). So I post when I have a question. I always find
> it amazing how fast the answers arrive. Sometimes in an hour or so.
> I did receive one or two cynical responses once, and I couldn't figure
> out why. Apparently some have a solid idea of what questions and
> comments should be posted, and which should not.
> I'm used to say to colleagues at work that there are no silly
> questions. There are only silly answers.- Hide quoted text -
>
> - Show quoted text -

Okay, I'm not trying to purify the forum here. And of course I can't
say that I've never asked a silly question before.

When your post came up, I did have an urge to answer it: "well, find
works on sequences, and member is specifically for lists, and
obviously they have different return values, as you must have seen
from what you typed in your post..." blah blah blah. But there's a
spec out there that goes into a lot more detail than I could, and does
it in a reasonably simple way. If you really wanted to know what the
functions did, it would be a lot faster to just read that document
than post here. Even if you are recieving answers as fast as "in an
hour or so"

Kyle
From: iu2
Subject: Re: find an element in a list
Date: 
Message-ID: <1190545637.523611.83750@57g2000hsv.googlegroups.com>
> Okay, I'm not trying to purify the forum here. And of course I can't
> say that I've never asked a silly question before.
>
> When your post came up, I did have an urge to answer it: "well, find
> works on sequences, and member is specifically for lists, and
> obviously they have different return values, as you must have seen
> from what you typed in your post..." blah blah blah. But there's a
> spec out there that goes into a lot more detail than I could, and does
> it in a reasonably simple way. If you really wanted to know what the
> functions did, it would be a lot faster to just read that document
> than post here. Even if you are recieving answers as fast as "in an
> hour or so"
>
> Kyle- Hide quoted text -
>
> - Show quoted text -

Well, I believe my question wasn't clear. I didn't mean to ask what
the differences are. For this I can look at the documentation (of
which I have planty). I thought that if there are several similar
methods to do the same thing, then there must be some *fundamental*
difference I probably missed (perhaps speed..).
The first answer in this page gave the answer I needed - no
fundamental difference, that's Lisp..
From: Kent M Pitman
Subject: Re: find an element in a list
Date: 
Message-ID: <y7exp2ll.fsf@nhplace.com>
iu2 <·······@elbit.co.il> writes:

> I thought that if there are several similar methods to do the same
> thing, then there must be some *fundamental* difference I probably
> missed (perhaps speed..).  The first answer in this page gave the
> answer I needed - no fundamental difference, that's Lisp..

No, the issues are primarily not speed.  They are things like the form
of the return value, the option to specify defaults and predicates,
and other such bells and whistles... and the fact that Lisp has
traditionally had a MEMBER function that occurs in a lot of the
literature and we didn't want to make go away while at the same time
giving it an upgrade.  So change is accommodated in the language gently
at the cost of some visual/motivational confusion in cases such as you raise.
From: Barry Margolin
Subject: Re: find an element in a list
Date: 
Message-ID: <barmar-2013A0.23512319092007@comcast.dca.giganews.com>
In article <·······················@19g2000hsx.googlegroups.com>,
 iu2 <·······@elbit.co.il> wrote:

> Hi all,
> 
> Hi all,
> Is there an essential differenct between assoc, member and find?
> 
> CL-USER> (setf lis1 '((A . 1) (B . 2) (C . 3)))
> ((A . 1) (B . 2) (C . 3))
> CL-USER> (assoc 'b lis1)
> (B . 2)
> 
> CL-USER> (setf lis2 '((A 1) (B 2) (C 3)))
> ((A 1) (B 2) (C 3))
> CL-USER> (member 'C lis2 :key #'car)
> ((C 3))

Try: (member 'b lis2 :key #'car)

> CL-USER> (find 'b lis2 :key #'car)
> (B 2)
> 
> thanks
> iu2

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***