chickadee » combinatorics » ordered-subset-for-each

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

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

f
The function to call
list
The list to permute
k
k distinct elements (default: n)
(define ordered-subset-for-each
  (case-lambda
    ((f list) (ordered-subset-for-each f list (length list)))
    ((f list k)
     (let iter ((list list) (k k) (subset '()))
       (if (zero? k)
         (f subset)
         (for-each
           (lambda (element)
             (iter (delete element list) (sub1 k) (cons element subset)))
           list))))))