Commit 1a229b8e authored by Juergen Nickelsen's avatar Juergen Nickelsen
Browse files

lisp problem 26/99

parent c43b20e6
......@@ -442,16 +442,28 @@
;; ((A B C) (A B D) (A B E) ... )
(defun combination (n l)
(let ((len (length l))
acc newlist)
(dotimes (i (- len (1- n)))
(let* ((rest (nthcdr i l))
((resta . restd) rest))
(dolist (combi (combination (1- n) restd))
(push (cons resta combi) acc))))
"Return all combinations of N elements of list L, leftmost first.
If N ist smaller than the length of L, return L."
(let ((len (length l)))
(if (<= len n)
(list l)
(if (zerop n)
(list nil)
(let (((first . rest) l))
(nconc (map (lambda (lrest) (cons first lrest))
(combination (1- n) rest))
(combination n rest)))))))
(test-is "lisp problem 26/99" (combination 3 '(a b c d e f))
'((a b c) (a b d) (a b e) (a b f)
(a c d) (a c e) (a c f)
(a d e) (a d f)
(a e f)
(b c d) (b c e) (b c f)
(b d e) (b d f)
(b e f)
(c d e) (c d f)
(c e f)
(d e f)))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment