s-99-26


S-99-26 Generate the combinations of K distinct objects chosen from the N elements of a list

Example:

 (combination 3 '(a b c d)) 
 ; => ((b c d) (a c d) (a b d) (a b c)) 

Solution:

 (define (combination k xs) 
   (cond [(null? xs) '()] 
         [(= k 1) (map list xs)] 
         [else (append (map (lambda (x) (cons (car xs) x)) 
                            (combination1 (- k 1) (cdr xs))) 
                       (combination1 k (cdr xs)))]))