This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

function SEARCH

4 posts · 1501 views

Hello,

Was looking for a good function to use to find elements and stumbled across SEARCH in the HyperSpec.

search sequence-1 sequence-2 &key from-end test test-not key start1 start2 end1 end2

I understand how search arrives at these results:

(search "dog" "it's a dog's life") => 7

(search '(0 1) (list 0 2 0 3 0 4 0 1 0 5)) => 6

But for some reason I cannot figure this one out, when :key is used:

(search '(0 1) '(2 4 6 1 3 5) :key #'oddp) => 2


I know :key is given a function, which in this case is asking if the element is odd, and I know that search returns the position in the second sequence in which the subsequence of the first sequence was found. How is the answer 2? To me it looks like this subsequnce is not in the second sequence at all, and definitely not in position 2 (where 6 is currently sitting).

Would love to have a good explanation of how this is working, thanks!

Re: function SEARCH

Well, I did not actually know that before, but apparently 17.2.1 specifies that if the sequence function operates on multiple sequences, which includes SEARCH, then :key is applied to elements of all sequences. Which means in this case you get an equivalent (although the point of :key is that no new lists are necessarily consed):
(search '(t nil) '(t t t nil nil nil))

Re: function SEARCH

Ramarren, you inverted the T's and NIL's. Anyway, your explanation is still valid.

Re: function SEARCH

Oops. But I will leave it, since it doesn't really matter and I don't like editing posts this old.