function SEARCH

Discussion of Common Lisp
Post Reply
I X Code X 1
Posts: 59
Joined: Sun May 29, 2011 8:52 pm
Location: NY
Contact:

function SEARCH

Post by I X Code X 1 » Wed Jun 29, 2011 8:29 pm

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!

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: function SEARCH

Post by ramarren » Wed Jun 29, 2011 10:36 pm

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):

Code: Select all

(search '(t nil) '(t t t nil nil nil))

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: function SEARCH

Post by gugamilare » Fri Jul 01, 2011 8:44 am

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

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: function SEARCH

Post by ramarren » Fri Jul 01, 2011 9:42 am

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

Post Reply