Page 1 of 1
Search a list of values in a list
Posted: Sun Apr 16, 2023 3:02 pm
by cameyo
What is the best method to search for a list of values in a list?
Example:
(set 'pat '(1 4 6))
(set 'lst '(2 2 2 1 4 6 2 2 2 1 4 6 2 2 1))
(ref-lst pat lst) ==> 3 ;the first index
and/or
(ref-all-lst pat lst) ==> (3 9) ;all indexes
Re: Search a list of values in a list
Posted: Mon Apr 17, 2023 12:25 am
by ralph.ronnquist
Code: Select all
(find pat lst (fn (needles item) (member item needles)))
find-all doesn't work for that though
with ref/ref-all:
Code: Select all
(ref pat lst (fn (needles item) (member item needles))))
(ref-all pat lst (fn (needles item) (member item needles)))
It might need "flat" on the ref-all but ref/ref-all also traverses the lst recursively if it's deeper.
EDIT: correction
Re: Search a list of values in a list
Posted: Mon Apr 17, 2023 6:14 pm
by cameyo
Thanks Ralph.
But I would like to find the starting index of the sequence 1 4 6, not the single number indices.
Re: Search a list of values in a list
Posted: Mon Apr 17, 2023 11:26 pm
by ralph.ronnquist
Ah, I misunderstood. That's slightly more convoluted. I suppose one approach could be something like the following:
Code: Select all
(filter (fn (i) (= pat (i (length pat) lst))) (flat (ref-all (pat 0) lst)))
Re: Search a list of values in a list
Posted: Tue Apr 18, 2023 8:15 am
by cameyo
This solves my question.
Thanks again Ralph.
Have a nice day.