This page contains Lisp functions I've written that I think may be of general use. These functions are provided with no license restrictions and no warranty.


[Function] n-combination

Generates a set of combinations containing indexes to a list.
n
0-indexed count to generate combination sets.
example: (n-combination (1- (length '(a b c d)))) --> ((0) (1) (0 1) (2) (0 2) (1 2) (0 1 2) (3) (0 3) (1 3) (0 1 3) (2 3) (0 2 3) (1 2 3) (0 1 2 3))
see also: index-append-combination
(defun n-combination (n)
 (if (> n 0)
  (let ((child (n-combination (1- n))))
   (append
    child
    (list (list n))
    (mapcar (lambda (x) (append x (list n))) child)
  ))
  '((0))
))

[Function] index-append-combination

Generates a list of combinations from the combination list.
comb
A list of index lists.
lst
A list of lists to permute.
example: (index-append-combination '((0) (1) (0 1) (2) (0 2) (1 2) (0 1 2)) '((a) (b) (c d))) --> ((A) (B) (A B) (C D) (A C D) (B C D) (A B C D))
note: This can be used with a list of items by changing the loop clause 'append' to 'collect'.
see also: n-combination
(defun index-append-combination (comb lst)
 (loop for c in comb
  collect 
  (loop for i in c
   append (nth i lst)
)))

Back
Change log: 6/17/05 - created page. added n-combination, index-append-combination