Iterating through a list repeatedly?

Discussion of Common Lisp
Post Reply
Cass
Posts: 6
Joined: Thu Oct 24, 2013 8:14 am

Iterating through a list repeatedly?

Post by Cass » Fri Jan 10, 2014 8:13 am

Hi again,

I'm trying to make something that iterates through two lists, matching the contents - though not the sequence - of the former against the latter returning true or false depending on whether it finds a match.

Code: Select all

(defun included (list lof-l)
  (if (not (eq (car list) (car lof-l))) ;; If car lst isn't car lof-l then
    (if (not (eq lof-l 'nil)) ;; If the second list isn't empty then
	(included list (cdr lof-l)) ;; Go to the next symbol in second list
	(if (not (eq list 'nil)) ;; If you're out of symbols in the second list
	    (included (cdr list) lof-l) ;; Go back to the start and go to the next symbol in the first list - no work!
	    'nil) ;; If you're out of symbols in the first list, you're finished
    't)) ;; If the symbol matches the symbol in the 2nd list you're finished
The puzzle, at least the moment, is that lof-l is getting munched on up until the point that I get to the end of it, and then I want to start with the next symbol in list starting over from the beginning of lof-l. But I've already eaten lof-l, and I don't know how to get it back.

I could keep track of the original form of lof-l as a global variable, or I could pass it down as a third variable in the function declaration, but both of those feel a bit off. The former because I can't see any other use for the thing, the latter because it seems odd to make someone input something twice.

I'm wondering if there's a more elegant way to have my lists and eat them. ^^;

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: Iterating through a list repeatedly?

Post by Goheeca » Fri Jan 10, 2014 5:10 pm

Cass wrote:matching the contents - though not the sequence
So what are you trying to achieve? The set-difference or a multiset analogy?
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: Iterating through a list repeatedly?

Post by nuntius » Fri Jan 10, 2014 8:26 pm

It sounds like you are asking for something like (maplist (lambda (x) (included x lof-l)) list) or maybe with mapl or mapcon.

Another useful pattern is to write an entry function that takes the primary arguments in a simple form and does some simple work before calling the recursive core.
In your case, this could mean having (included list lof-l) call (included-helper list lof-l list).
When you don't want the separate helper function, use flet.
If you have multiple "internal" helper functions that call each other, use labels

CLHS links:
http://www.lispworks.com/documentation/ ... _mapc_.htm
http://www.lispworks.com/documentation/ ... _flet_.htm

Cass
Posts: 6
Joined: Thu Oct 24, 2013 8:14 am

Re: Iterating through a list repeatedly?

Post by Cass » Wed Jan 15, 2014 8:05 am

It works! ^_^

Thank you!

And for the general approach tips too :D

Post Reply