chickadee » combinatorics » unordered-subset-fold

unordered-subset-fold cons nil listprocedure
unordered-subset-fold cons nil list kprocedure

Recombine every k-combination (partial unordered subset) of list, starting with a base-case nil, and calling cons with 1. a combination and 2. the accumulated value.

cons
The combining function
nil
The base case
list
The list to recombine
k
k distinct elements (default: n)
(define unordered-subset-fold
  (case-lambda
    ((cons nil list) (unordered-subset-fold cons nil list (length list)))
    ((cons nil list k)
     (let ((nil (make-parameter nil)))
       (unordered-subset-for-each
         (lambda (subset) (nil (cons subset (nil))))
         list
         k)
       (nil)))))