summaryrefslogtreecommitdiff
path: root/combinations/combinations.scm
blob: 8817176408904fca45bcee39fb820dc4c1055f1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(define-module (combinations)
  #:export (combinations))


(define (combinations lst n)
  "Return N length subsequences of elements from
LST."
  (if (or (null? lst) (zero? n))
      (if (zero? n)'(()) '())
      (let loop ((lst lst) (result '()))
        (if (null? lst)
            result
            (let ((next-lst (cdr lst)))
	      (loop next-lst
                    (append result
                            (map (lambda (combo)
                                   (cons (car lst) combo))
                                 (combinations next-lst
					       (1- n))))))))))