Re: [SLUG] Re: [PIG] Enumerate possible permutations?

From: Dylan William Hardison (dylan@hardison.net)
Date: Thu Jul 20 2006 - 22:31:44 EDT


Here's a (non-pretty) version in haskell:

-- begin program --
-- Written by B. Donlan
permute :: [a] -> [[a]]
permute [] = [[]]
permute [x] = [[x]]
permute l = concat $ map pick [0..(length l) - 1]
              where pick n = map ((l !! n):) $ permute (take n l ++ drop (n + 1) l)

main = do
       putStrLn $ show $ permute [1 .. 10]
-- end program --

Note: the $ is a lot like the | in the unix shell, it's for making
      pipelines of functions.

It has a pretty good runtime when compiled with the glasgow haskell
compiler, with -O3:

% time ./permute > perm2
./permute > perm2 50.01s user 0.91s system 94% cpu 53.731 total

That's really good runtime considering it produced 3628800 permutations!
perm2 is around 80MB.

Can anyone guess what the memory consumption was?

-- 
Colvard's Unconscionable Commentary:
        This is especially true when dealing with someone you're attracted to.
 
Grelb's Commentary:
        Likelihoods, however, are 90% against you.
-
GPG Fingerprint: E3CD FDAB 82C4 14FD 7B57  430B 770E 0EAF FB53 12C2
-----------------------------------------------------------------------
This list is provided as an unmoderated internet service by Networked
Knowledge Systems (NKS).  Views and opinions expressed in messages
posted are those of the author and do not necessarily reflect the
official policy or position of NKS or any of its employees.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 14:52:05 EDT