John Cupitt wrote:
> }(defun permutations (elements)
> } (if (null elements)
> } (list nil)
> } (mapcan
> } #'(lambda (element)
> } (mapcar
> } #'(lambda (permutation)
> } (cons element permutation))
> } (permutations (remove element elements))))
> } elements)))
> }
> }Comments? Alternatives?
>
> Good grief! I almost collapsed when I saw this .. I realise that this is a
> LISP group, but I can't resist posting the same algorithm in Miranda:
>
> perms :: [*] -> [[*]]
> perms x = [[]], x = []
> = [ a:p | a <- x; p <- perms (x--[a]) ], otherwise
(require srfi/42) ; list-ec
(define (perms xs)
(if (null? xs)
'(())
(list-ec (: x xs) (: p (perms (remove x xs)))
(cons x p))))
> (perms '(b))
'((b))
> (perms '(b c))
'((b c) (c b))
> (perms '(b c d))
'((b c d) (b d c) (c b d) (c d b) (d b c) (d c b))
In Forth?
--
Mr. Porter disclosed the fact that his ... staff in the Embassy ... had ...
decoded communications between the commander ... and the Israeli High Command,
which proved that the latter knew that the Liberty was an unarmed American
naval vessel, and ... ordered that the American ship be attacked and sunk.
http://archive.org/details/nolies