; P26: Generate the combinations of K distinct objects chosen from the N
; elements of a list
(define p26-combination
(lambda (k lst)
(cond
((or (> k (length lst)) (< k 1)) '())
((= k 1) (cons (list (car lst)) (p26-combination k (cdr lst))))
(else
(append
(p26-combination.combine (car lst) (p26-combination (- k 1) (cdr lst)))
(p26-combination k (cdr lst)))))))
(define p26-combination.combine
(lambda (head tuples)
(if (null? tuples)
'()
(cons (cons head (car tuples)) (p26-combination.combine head (cdr tuples))))))
(test '((a) (b) (c) (d)) '(p26-combination 1 '(a b c d)))(test '((a b) (a c) (a d) (b c) (b d) (c d)) '(p26-combination 2 '(a b c d)))(test '((a b c) (a b d) (a c d) (b c d)) '(p26-combination 3 '(a b c d)))
(p26-combination 1 heads) ; ex: turns ((a b) (a c) (b c)) into (((a b)) ((a c)) ((b c)))
(append ; instead of a append, for efficiency, could define a function like p26-combination.combine, but that adds on to a running result of p27-group.2
(p26-combination.combine (car heads) tails)
(p27-group.2 lst ks (cdr heads))))))))
(define p27-remove-multi ; removes first instance of each `els` from `lst`