0 Members and 1 Guest are viewing this topic.
(GetPermutations '(a b c))==> ((a b c) (a c b) (b a c) (b c a) (c a b) (c b a))
(defun LM:GetPermutations ( l / f1 f2 ) (defun f1 ( l ) (if (cdr l) (apply 'append (mapcar 'f2 l)) (list l) ) ) (defun f2 ( a ) (mapcar '(lambda ( b ) (cons a b)) (f1 (vl-remove a l))) ) (f1 l))
(GetPermutations '(a b))==> ((a b) (b a))
(defun test (l) (if (cdr l) (apply 'append (mapcar '(lambda (a) (mapcar '(lambda (b) (cons a b)) (test (vl-remove a l)))) l) ) (list l) ))
Lee, maybe you'll find something interesting herehttp://www.theswamp.org/index.php?topic=14831.15
(defun LM:GetPermutations2 ( l / f1 f2 f3 f4 ) (defun f1 ( l ) (if (cdr l) (apply 'append (mapcar 'f2 l)) (list l) ) ) (defun f2 ( a ) (mapcar '(lambda ( b ) (cons a b)) (f1 (f3 a l))) ) (defun f3 ( x l ) (if l (if (equal x (car l)) (cdr l) (cons (car l) (f3 x (cdr l))) ) ) ) (defun f4 ( l ) (if l (cons (car l) (f4 (vl-remove (car l) l)))) ) (f4 (f1 l)))
(LM:GetPermutations2 '(A A B))==> ((A A B) (A B A) (B A A))
(defun t1 (l) (if (cdr l) (t3 (apply 'append (mapcar '(lambda (a b) (mapcar '(lambda (b) (cons a b)) (t1 b))) l (t2 nil l)) ) ;_ apply ) ;_ t3 (list l) ) ;_ if) ;_ defun(defun t2 (a b) (if b (cons (append a (cdr b)) (t2 (append a (list (car b))) (cdr b))) ) ;_ if) ;_ defun(defun t3 (l) (if l (cons (car l) (t3 (vl-remove (car l) (cdr l)))) ) ;_ if) ;_ defun
(t1 '(1 2 3));=>> ((1 2 3) (1 3 2) (2 1 3) (2 3 1) (3 1 2) (3 2 1))(t1 '(1 1 1 2));=>> ((1 1 1 2) (1 1 2 1) (1 2 1 1) (2 1 1 1))
Code - Auto/Visual Lisp: [Select](defun permute-n < ... >)
Thanks Lee. You're right about my function giving duplicates - that's one of the reasons I was searching for a solution. At the time my mind wasn't working :-[ , but your codes have given me some new ideas - thanks again.