combinations   scheme


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

Implementation

;; return all combinations of set
;; or subsets (size) of set
(define combinations
   (lambda (s size)
      (if (< (length s) size)
    '()
    (letrec ((f1 (lambda (h t)
        (if (= (length h) size)
            (reverse h)
            (let loop ((e (car t))
           (l (cdr t)))
         (if (null? l)
             (if (< (length h) (- size 1)) '()
           (reverse (list* e h)))
             (list (f1 (list* e h) l)
             (loop (car l) (cdr l)))))))))
       (cl:remove '() (flatten-1 (append (f1 (list (car s)) (cdr s))
                 (combinations (cdr s) size))))))))


Back to Index

Similar Entries

  • combinations    scheme