Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ukma!husc6!rice!titan!dorai From: dorai@titan.rice.edu (Dorai Sitaram) Newsgroups: comp.lang.lisp Subject: Re: lisp problem Keywords: help Message-ID: <3079@kalliope.rice.edu> Date: 11 Apr 89 05:15:27 GMT References: <10053@ihlpl.ATT.COM> Sender: usenet@rice.edu Reply-To: dorai@titan.rice.edu (Dorai Sitaram) Organization: Rice University, Houston Lines: 34 In article <10053@ihlpl.ATT.COM> canoura@ihlpl.ATT.COM (jlcanoura) writes: $PLEASE, does anybody know how to solve this problem in LISP. $ $By using recursion and the Append function, Given a list of $nondistinct elements and a positive number K where K is <= $the size of the list, I need to write a function which returns $a list, the element of whose are the combinations of the $element of the given list taken K at a time. $e.g.(COMB '(n1 n2 ....n) K) $ $Examples. ( COMB is the name of the function) $ $(COMB '(1 2 3 ) 2) should return $ ((1 2) (1 3) (2 3)) $ $(COMB '(1 2 3) 3) should return $ ((1 2 3)) $ $(COMB '(1 2 3) 1) should return $ ((1) (2) (3)) (defun comb (s k) (cond ((zerop k) (list nil)) ((null s) nil) (t (let ((a (car s)) (s1 (cdr s))) (append (mapcar #'(lambda (c) (cons a c)) (comb s1 (1- k))) (comb s1 k)))))) Regards, --dorai ------------------------------------------------------------------------------ We must believe in free will. We have no choice. --Isaac Bashevis Singer ------------------------------------------------------------------------------