(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))))))))))