permutations   scheme


Defined in:  https://github.com/digego/extempore/tree/v0.8.9/runtime/scheme.xtm

Implementation

;; return all permutations of set
;; of subsets (size) of set
(define permutations
   (lambda (set size)
      (if (null? set)
    '(())
    (letrec ((f1 (lambda (s)
        (map (lambda (e)
          (f2 (list e) (cl:remove e s)))
             s)))
       (f2 (lambda (e s)
        (if (= (length e) size)
            e
            (map (lambda (e2)
              (f2 (append e (list e2)) (cl:remove e2 s)))
           s)))))
       (flatten-1 (f1 set))))))


Back to Index