Lisp HUG Maillist Archive

Combinatorics question ...

Hi,

I'm beginer in Lisp programming and I'm trying to write a Lisp  
version of the function that I managed to code in Haskel using list  
comprehensions. The function given the list and the number x should  
return all subsets (without repetitions) of the given list that are  
exactly x element long - I think it's called variation without  
repetitions. For example :

(variations 2 '(1 2 3)) => ((1 2) (1 3) (2 3))
(variations 1 '(1 2 3)) => ((1) (2) (3))
(variations 3 '(1 2 3)) => ((1 2 3))
(variations 3 '(1 2 3 4)) => ((1 2 3) (1 2 4) (1 3 4) (2 3 4))

and so on...

I'm getting confused with mapcar's, mapcan's, maplist's - so can  
anybody help, event if it's just pointing the direction for research

regards
Krzys


RE: Combinatorics question ...

Krzys,

I found the following at http://paste.lisp.org/display/22379

(defun n-choose-k (list k &optional acc)
  (cond
    ((= k 0) (print acc))
    ((null list))
    (t (n-choose-k (cdr list) k acc)
       (n-choose-k (cdr list) (1- k) (cons (car list) acc)))))

Mitch


Updated at: 2020-12-10 08:46 UTC