Permutations: Difference between revisions

216 bytes removed ,  11 years ago
→‎{{header|Racket}}: We now have a library function, and also a faster+simpler naive impl
(→‎{{header|Racket}}: We now have a library function, and also a faster+simpler naive impl)
Line 2,440:
 
=={{header|Racket}}==
<lang racketRacket>#lang racket
#lang racket
 
;; using a builtin
(define (permutations ℓ)
(permutations '(A B C))
(if (empty? ℓ) (list ℓ)
;; -> '((A B C) (append-mapB A C) (λA C B) (eC A B) (mapB C A) (curryC consB eA))
 
(permutations (remove e ℓ))))
;; a random simple version (which is actually pretty good for a simple version)
ℓ)))</lang>
(define (permutationsperms l)
Try it:
(let loop ([l l] [tail '()])
<lang racket>(printf "Perumutations of ~a:\n~a\n\n" '(r o c) (permutations '(r o c)))
(if (emptynull? l) (list tail)
(printf "Time [in milliseconds] to generate permutations of 9 numbers:\n")
(append-map ( (x) (loop (remq x l) (cons x tail))) l))))
(time (void (length (permutations (range 0 9)))))</lang>
(perms '(A B C))
Output:
;; -> '((rC oB cA) (rB cC oA) (oC rA cB) (oA cC rB) (cB rA oC) (cA oB rC))
<pre>Perumutations of (r o c):
</lang>
((r o c) (r c o) (o r c) (o c r) (c r o) (c o r))
 
Time [in milliseconds] to generate permutations of 9 numbers:
cpu time: 1672 real time: 1719 gc time: 816</pre>
=={{header|REXX}}==
===names===