Last active
August 29, 2015 14:23
-
-
Save flq/aad90a6ce88a01b42513 to your computer and use it in GitHub Desktop.
No-frills permutation code in Haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Permutation where | |
permutate :: [a] -> [[a]] | |
permutate [x1,x2] = [[x1,x2],[x2,x1]] | |
permutate items = concat $ map p $ getEachInFrontOnce items | |
where | |
p (x:xs) = map (\items -> [x]++items) $ permutate xs | |
getEachInFrontOnce items = map (putItemAtIndexToFront items) [0..length items-1] | |
putItemAtIndexToFront items idx = [items!!idx] ++ take idx items ++ drop (idx+1) items |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment