
4 Jan
2006
4 Jan
'06
6:16 a.m.
And for sanity's sake, I wish one of the entries would have documentated a clear way to understand the permutation generator. The PHP and Lua versions are almost legible.
Here's a neat Haskell version: -- rotate initial n elements of the list left by one place rotate n (x:xs) = rot' n xs where rot' 1 xs = x:xs rot' n (x:xs) = x:rot' (n-1) xs permutations l = foldr perm' [l] [2..length l] where perm' n l = l >>= take n . iterate (rotate n) Combined with Jan-Willem Maessen's ideas (i.e. the single-pass flop) this runs about 85 times faster than the current shootout entry. Bertram