Hi, I wrote a function doing the following: it takes a list and an integer as arguments and returns a list of lists which are all different subsets of length n from the given list:
I would like to have your comments: can I improve the function (that I use very much; all advice to have quicker or anything better will be fine). [The sublists are not given in a "pretty" order, because I don't need it; since I take them are subsets (and not lists), (4 2) is for me the same as (2 4)]
(define (subsets l n) (let ((subsets* '())) (letrec ((subsets0 (lambda (l2 l* n2) (if (zero? n2) (set! subsets* (cons l* subsets*)) (do ((i 0 (+ i 1))) ((> i (- (length l2) n2)) subsets*) (subsets0 (list-tail l2 (+ i 1)) (cons (list-ref l2 i) l*) (- n2 1))))))) (subsets0 l '() n))))