[plt-scheme] On continuations...

From: Eli Barzilay (eli at barzilay.org)
Date: Sat Dec 18 00:38:52 EST 2004

Yet another solution -- one that still doesn't explain about
continuation, but instead a _direct_ translation of the original
Python using Swindle's (which is now included with v209) loops is:

======================================================================
(define (partitions n)
  (list-of x
    (x <- (lambda (yield)
            (if (zero? n)
              (yield null)
              (loop-for (p <- (partitions (- n 1))) do
                (yield (add '(1) p))
                (when (and (not (null? p))
                           (or ((len p) . < . 2)
                               ((ref p 1) . > . (ref p 0))))
                  (yield (add (list (+ (ref p 0) 1)) (cdr p))))))))))
======================================================================

Which you can test with

  (loop-for (x <- (partitions 6)) (echo x))

or

  (echo :\{ (partitions 6) :n :\})

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!



Posted on the users mailing list.