Skip to content

Instantly share code, notes, and snippets.

@flq
Last active August 29, 2015 14:23
Show Gist options
  • Save flq/aad90a6ce88a01b42513 to your computer and use it in GitHub Desktop.
Save flq/aad90a6ce88a01b42513 to your computer and use it in GitHub Desktop.
No-frills permutation code in Haskell
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