Skip to content

Instantly share code, notes, and snippets.

@vitapluvia
Created July 3, 2017 01:47
Show Gist options
  • Save vitapluvia/98d8641a1ecf75d7da4c6f471318e81d to your computer and use it in GitHub Desktop.
Save vitapluvia/98d8641a1ecf75d7da4c6f471318e81d to your computer and use it in GitHub Desktop.
permutations in haskell
import Data.List (nub)
disperse :: a -> [a] -> [[a]]
disperse value arr =
map (\(start, ending) -> start ++ [value] ++ ending) .
map (\char -> splitAt char arr) $ [0..length arr]
permu :: Ord a => [a] -> [[a]]
permu [] = [[]]
permu (x:xs) = concatMap (disperse x) $ permu xs
main = mapM_ putStrLn $ nub $ permu ['a'..'d']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment