From 6f98e0d38fbb3d282a627ba70ddeb9977815bb4c Mon Sep 17 00:00:00 2001 From: bd Date: Sun, 9 Jun 2024 17:10:45 -0600 Subject: Implementation and tests for "combinations" function --- combinations/combinations.scm | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 combinations/combinations.scm (limited to 'combinations/combinations.scm') 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)))))))))) -- cgit v1.2.3