Skip to content

Instantly share code, notes, and snippets.

@dionyziz
Created April 5, 2019 08:11
Show Gist options
  • Save dionyziz/213ac58311d07ef7127d5fe8e7a721c4 to your computer and use it in GitHub Desktop.
Save dionyziz/213ac58311d07ef7127d5fe8e7a721c4 to your computer and use it in GitHub Desktop.
split :: [a] -> [[a]]
split xs = f ([], []) xs
where f :: ([a], [[a]]) -> [a] -> [[a]]
f (seen, acc) [] = acc
f (seen, acc) (x:xs) = f (seen ++ [x], acc ++ [x:(seen ++ xs)]) xs
perms :: [a] -> [[a]]
perms [] = [[]]
perms xs = foldl f [] $ split xs
where f :: [[a]] -> [a] -> [[a]]
f acc (x:xs) = acc ++ (map (x:) (perms xs))
main = print $ perms "abc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment