Permutations: Difference between revisions
Content added Content deleted
(→{{header|Racket}}: Added.) |
(→{{header|Racket}}: We now have a library function, and also a faster+simpler naive impl) |
||
Line 2,440: | Line 2,440: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<lang |
<lang Racket> |
||
#lang racket |
|||
;; using a builtin |
|||
⚫ | |||
(permutations '(A B C)) |
|||
⚫ | |||
;; -> '((A B C) (B A C) (A C B) (C A B) (B C A) (C B A)) |
|||
(permutations (remove e ℓ)))) |
|||
;; a random simple version (which is actually pretty good for a simple version) |
|||
ℓ)))</lang> |
|||
⚫ | |||
Try it: |
|||
(let loop ([l l] [tail '()]) |
|||
<lang racket>(printf "Perumutations of ~a:\n~a\n\n" '(r o c) (permutations '(r o c))) |
|||
⚫ | |||
(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: |
|||
⚫ | |||
<pre>Perumutations of (r o c): |
|||
</lang> |
|||
⚫ | |||
Time [in milliseconds] to generate permutations of 9 numbers: |
|||
cpu time: 1672 real time: 1719 gc time: 816</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===names=== |
===names=== |