chickadee » combinatorics » unordered-subset-for-each

unordered-subset-for-each f listprocedure
unordered-subset-for-each f list kprocedure

Iterate over every k-combination (partial unordered subset) of list, calling f for its side effect.

The function to call
The list to permute
k distinct elements (default: n)
(define unordered-subset-for-each
    ((f list) (unordered-subset-for-each f list (length list)))
    ((f list k)
     (let ((subset (make-vector k)) (n (length list)))
       (let iter ((start 0) (p 0))
         (if (= p k)
           (f (project subset list))
           (do ((i start (+ i 1)))
               ((= i n))
             (vector-set! subset p i)
             (iter (add1 i) (add1 p)))))))))