Norbert_Paul wrote:
> Gerhard wrote:
>> Hello,
>>
>> The first call will get what I intended, but ...
> So what do you intend then?
>
What I want is a version of "search" that does not only tell me where the
first occurrence of a given sequence is, but that gives me the position of
all occurrences as a list of integers.
So, instead of:
CL-USER> (search '("a") '("a" "b" "c" "d" "a" "a") :test #'string-equal)
0
I would like to have
CL-USER> (search-all '("a") '("a" "b" "c" "d" "a" "a"))
(0 4 5)
> A hint: Try to figure out, what "lexical closures" are. This might help
> you to understand.
Have checked, and the following seems to work:
(defun search-all-1 (a b &key (test #'equal))
(let ((lst))
(labels ((f (a b acc)
(let ((x (search a b :test test)))
(if x
(progn
(push (+ x acc) lst)
(incf acc (+ 1 x))
(f a (subseq b (1+ x)) acc))
(nreverse lst)))))
(f a b 0))))
I think that I understand now why and how the above works. But I still do
not understand the behaviour of the preceding version.
I would have expected that - if the (let ((lst)) had the defun in its scope
- a new call of the function did not affect the content of the variable lst.
Yet a new call seems to purge all but the last element of the preceding call
to it. Where am I wrong?