diff options
Diffstat (limited to 'combinations/combinations.scm')
-rw-r--r-- | combinations/combinations.scm | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/combinations/combinations.scm b/combinations/combinations.scm new file mode 100644 index 0000000..8817176 --- /dev/null +++ b/combinations/combinations.scm @@ -0,0 +1,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)))))))))) |