diff options
author | bd <bdunahu@operationnull.com> | 2024-06-09 17:10:45 -0600 |
---|---|---|
committer | bd <bdunahu@operationnull.com> | 2024-06-09 17:10:45 -0600 |
commit | 6f98e0d38fbb3d282a627ba70ddeb9977815bb4c (patch) | |
tree | f389167da17a5e2124f45cfcd0a70b181c9fdad3 /combinations/combinations.scm | |
parent | cd47b52a406ebed5b3f82e51168ead27af3795d9 (diff) |
Implementation and tests for "combinations" function
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)))))))))) |