[comp.lang.lisp] assoc in KCL

whaley@june.cs.washington.edu (Ken Whaley) (11/05/88)

Much to my dismay, I've found that the KCL we have here does NOT have
the keyword ":key" in the "assoc" function.  Since I obviously have to write
my own (I suppose there's the danger of replicating "the best way to sum
a list" discussion....I'm not terribly interested in the "best" way of doing
this, I just want to know the "right" way).
CLtL isn't clear as to how assoc uses the keywork ":key":


	assoc	item	a-list     &key :test :test-not :key	[Function]


I.e., exactly which things does :key get applied to?  In Allegro 3.0,
it seems to apply only to the items in the a-list.  I.e.,

(assoc 'a '( ((c) . d) ((a) . b)) :key #'car :test #'equal) ==> ((a) . b)


Is this correct?

			Ken
-- 
whaley@june.cs.washington.edu

cox@renoir.Berkeley.EDU (Charles A. Cox) (11/11/88)

[ I am forwarding this to the net from Franz Inc. ]

In article <6341@june.cs.washington.edu> whaley@uw-june.UUCP (Ken Whaley) writes:
>
>CLtL isn't clear as to how assoc uses the keywork ":key":
>
>
>	assoc	item	a-list     &key :test :test-not :key	[Function]
>
>
>I.e., exactly which things does :key get applied to?  In Allegro 3.0,
>it seems to apply only to the items in the a-list.  I.e.,
>
>(assoc 'a '( ((c) . d) ((a) . b)) :key #'car :test #'equal) ==> ((a) . b)
>
>
>Is this correct?

That is the correct behavior.  :key is defined on page 246,
where it says:

   If an operation tests elements of a sequence in any
   manner, the keyword argument :key, if not nil, should be
   a function of one argument that will extract from an
   element the part to be tested in place of the whole
   element.  For example, the effect of the MacLisp
   expression (aasq item seq) could be obtained by
     
     (find item sequence :test #'eq :key #'car)
   
   This searches for the first element of sequence whose car
   is equal to item.

From that, the normal behavior of the :key argument is that
it is applied to the sequence elements but not to item.
There are two functions, pushnew and adjoin, where the value
of the :key argument is applied to item as well.  The
description of adjoin (p. 276) says:
   
   adjoin deviates from the usual rules described in chapter
   14 for the treatment of arguments named item and key.  If
   a key function is specified, it is applied to item as
   well as each element of the list.

No such deviation is reported for assoc (p. 280).

David Margolies
Franz Inc.