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
Search a list of values in a list
Search a list of values in a list
Last edited by cameyo on Mon Apr 17, 2023 6:13 pm, edited 1 time in total.
-
- Posts: 228
- Joined: Mon Jun 02, 2014 1:40 am
- Location: Melbourne, Australia
Re: Search a list of values in a list
Code: Select all
(find pat lst (fn (needles item) (member item needles)))
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)))
EDIT: correction
Re: Search a list of values in a list
Thanks Ralph.
But I would like to find the starting index of the sequence 1 4 6, not the single number indices.
But I would like to find the starting index of the sequence 1 4 6, not the single number indices.
-
- Posts: 228
- Joined: Mon Jun 02, 2014 1:40 am
- Location: Melbourne, Australia
Re: Search a list of values in a list
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
This solves my question.
Thanks again Ralph.
Have a nice day.
Thanks again Ralph.
Have a nice day.