Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

permutations

27 views
Skip to first unread message

Robert L.

unread,
May 11, 2018, 7:06:36 PM5/11/18
to
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
0 new messages