Download code

Jump to: navigation, search

Back to Kth_permutation_(Standard_ML)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

input.nw

 1 <<radix representation>>=
 2 fun rr (0, _) = []
 3   | rr (n, k) = k mod n :: rr (n-1, k div n)
 4 
 5 @ text
 6 
 7 <<direct representation from radix rep>>=
 8 val dfr = foldr (fn (x, rs) => x :: map (fn r => r + (if x <= r then 1 else 0)) rs) []
 9 @ text
10 
11 <<generate permutation>>=
12 fun perm (xs, k) = map (fn i => List.nth (xs, i)) (dfr (rr (length xs, k)))
13 @ text
14 
15 <<parity from radix rep>>=
16 fun par rs = foldl op+ 0 rs mod 2
17 @ text
18 
19 <<kthperm.sml>>=
20 <<radix representation>>
21 <<direct representation from radix rep>>
22 <<parity from radix rep>>
23 <<generate permutation>>
24 <<utility functions>>
25 ;
26 
27 app print
28   (List.tabulate (24, fn k => implode (perm (explode "perm", k)) ^
29                               " " ^ Int.toString (par (rr (4, k))) ^ "\n"));
30 
31 @ text
32 


hijacker
hijacker
hijacker
hijacker

kthperm.sml

 1 (* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Kth_permutation_(Standard_ML)?oldid=16318
14 *)
15 
16 fun rr (0, _) = []
17   | rr (n, k) = k mod n :: rr (n-1, k div n)
18 
19 val dfr = foldr (fn (x, rs) => x :: map (fn r => r + (if x <= r then 1 else 0)) rs) []
20 fun par rs = foldl op+ 0 rs mod 2
21 fun perm (xs, k) = map (fn i => List.nth (xs, i)) (dfr (rr (length xs, k)))
22 
23 ;
24 
25 app print
26   (List.tabulate (24, fn k => implode (perm (explode "perm", k)) ^
27                               " " ^ Int.toString (par (rr (4, k))) ^ "\n"));
28 


hijacker
hijacker
hijacker
hijacker

noweb.log

1 undefined chunk name: <<utility functions>>