Re: combinations iterator
- From: Pascal Bourguignon <pjb@xxxxxxxxxxxxxxxxx>
- Date: Tue, 02 Jan 2007 08:52:13 +0100
"mark.hoemmen@xxxxxxxxx" <mark.hoemmen@xxxxxxxxx> writes:
On Jan 1, 2:47 pm, Pascal Bourguignon <p...@xxxxxxxxxxxxxxxxx> wrote:
http://darcs.informatimago.com/darcs/public/lisp/common-lisp/combinat...
what i was thinking of for the second question (generalizing the idea
of iterator over a process) was something like this. If you want a
list of all the combinations of the elements of a given list, you do
something like this:
(defun combinations (list)
(cond ((null list) nil)
((atom list) (combinations (list list)))
((null (cdr list)) (cons list nil))
(t (append (cons (first list) (combinations (cdr list)))
(combinations (cdr list))))))
However, to get an iterator over the output list without actually
constructing the output list, we need some new operators. Suppose
(YIELD x) means "the value x should pop up the chain of YIELDs back to
the iterator, and when the chain ends, the result should be returned
now by the iterator," and (SEQ x y z ...) means "the iterator should
follow the sequence x, y, z if x, y, z, etc. YIELD something." Then we
could write:
(defun combinations (list)
(cond ((null list) (yield nil))
((atom list) (yield (combinations (list list))))
((null (cdr list))
(seq (yield list) (yield nil)))
(t (seq (yield (cons (first list) (combinations (cdr
list))))
(yield (combinations (cdr list)))))))
well, something like that; I'm not being very precise. The idea is
that you just write your algorithms like you would before, but instead
of collecting data into a data structure, you YIELD each datum as you
generate it. I know the SERIES package has something vaguely like the
YIELD operator; could it be (efficiently) adapted to the above case?
Or is this a job for continuations?
Yes, continuations, or coroutines. It's possible that partial
continuations we can implement in CL would be enough for this.
--
__Pascal Bourguignon__ http://www.informatimago.com/
"What is this talk of "release"? Klingons do not make software
"releases". Our software "escapes" leaving a bloody trail of
designers and quality assurance people in its wake."
.
- References:
- combinations iterator
- From: mark.hoemmen@xxxxxxxxx
- Re: combinations iterator
- From: Pascal Bourguignon
- Re: combinations iterator
- From: mark.hoemmen@xxxxxxxxx
- combinations iterator
- Prev by Date: Re: CLISP + xinetd = Instant failure.
- Next by Date: Re: "Free" Software Strikes Again, or How I Ended Up With My Brother In My Airspace For Two Hours
- Previous by thread: Re: combinations iterator
- Next by thread: Re: combinations iterator
- Index(es):
Relevant Pages
|