[Small-cl-src] Small, occasionally handy piece of code

Ingvar ingvar at cathouse.bofh.se
Fri Jul 9 12:01:38 UTC 2004


;;; Haven't we all felt the need of generating combinations from a collection
;;; and calling a function for every combination?
;;;
;;; It's about as easy as this:
;;; <list> is the initial set
;;; <num> is the number of elements in the resulting combination
;;; <func> is the callback function
(defun call-with-combination (list num func &optional acc)
  (declare (list list acc)
	   (fixnum num)
	   (function func))
  (if (zerop num)
      (apply func acc)
    (loop for elt in list
	  do (call-with-combination
	      (remove elt list) (1- num) func (cons elt acc)))))






More information about the Small-cl-src mailing list