Looking at the docs for #'adjoin in the HyperSpec, I see the following in the Examples section:
(setq slist '()) => NIL
(setq slist (adjoin '(test-item 1) slist)) => ((TEST-ITEM 1))
(adjoin '(new-test-item 1) slist :key #'cadr) => ((TEST-ITEM 1))
I would have expected the following, instead:
(adjoin '(new-test-item 1) slist :key #'cadr) => ((NEW-TEST-ITEM 1) (TEST-ITEM 1))
My expectation is due to the following text in the HyperSpec (17.2.1):
When an object O is being considered iteratively against each element Ei of a sequence S by an operator F listed in the next figure, it is sometimes useful to control the way in which the presence of O is tested in S is tested by F. This control is offered on the basis of a function designated with either a :test or :test-not argument.
and, further:
The object O might not be compared directly to Ei. If a :key argument is provided, it is a designator for a function of one argument to be called with each Ei as an argument, and yielding an object Zi to be used for comparison. (If there is no :key argument, Zi is Ei.)
The function designated by the :key argument is never called on O itself. However, if the function operates on multiple sequences (e.g., as happens in set-difference), O will be the result of calling the :key function on an element of the other sequence.
So we have slist (the sequence, S) as '((TEST-ITEM 1))
and O as '(new-test-item 1)
. To check if O should be adjoined, the function #'cadr
is applied to the element(s) of S, the first being '(test-item 1)
. So, that test gives:
(cadr '(test-item 1)) => 1
Now, when O, '(new-test-item 1)
, is checked against the result of applying #'cadr
to E1 of S with #'eql
(the equality function used when none is supplied with :test
), the false
result should mean that O is adjoined. At least that's what I would think. What am I misunderstanding?
This is a bug in the HyperSpec and Issue ADJOIN-SPECIFICATION has been written about it on the CLiki. The relevant parts are: