xach wrote
on September 29th, 2011 at 08:56 am

Permutation and list rotation

Here's an interesting implementation of a list permutation function, by Erik Naggum:

(defun permutations (list)
  (if (cdr list)
      (loop with rotation = list
          do (setq rotation (nconc (last rotation) (nbutlast rotation)))
          nconc (loop for list in (permutations (rest rotation))
                      collect (cons (first rotation) (copy-list list)))
          until (eq rotation list))
      (list list)))

The rotation trick with nconc/last/nbutlast/eq is new to me.

Tags:

(Read Comments)

No HTML allowed in subject

  
 
   
 

Notice! This user has turned on the option that logs your IP address when posting. 

(will be screened)

September 2014

S M T W T F S
 123456
78910111213
14151617181920
21222324252627
282930    
Powered by LiveJournal.com