[racket] "variadic"nested for?

From: Eli Barzilay (eli at barzilay.org)
Date: Tue Mar 13 22:12:46 EDT 2012

You can do this without a macro:

  -> (let loop ([depth 3] [l '()])
       (if (zero? depth)
         (printf "~s\n" (reverse l))
         (for ([i 2])
           (loop (sub1 depth) (cons i l)))))
  (0 0 0)
  (0 0 1)
  (0 1 0)
  (0 1 1)
  (1 0 0)
  (1 0 1)
  (1 1 0)
  (1 1 1)

Or to get a list of the results:

  -> (let loop ([depth 3] [l '()])
       (if (zero? depth)
         (list (reverse l))
         (append* (for/list ([i 2])
                    (loop (sub1 depth) (cons i l))))))
  '((0 0 0) (0 0 1) (0 1 0) (0 1 1) (1 0 0) (1 0 1) (1 1 0) (1 1 1))

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

Posted on the users mailing list.