Created
October 7, 2011 17:22
-
-
Save aristidb/1270845 to your computer and use it in GitHub Desktop.
Code to generate list permutations
This file contains hidden or 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
| import Data.Tree | |
| import Data.List | |
| import Control.Applicative | |
| permutationForest :: [a] -> Forest a | |
| permutationForest = zipWith3 build <*> inits <*> tail . tails | |
| where build a xs ys = Node a (permutationForest $ xs ++ ys) | |
| traverse :: Forest a -> [[a]] | |
| traverse [] = [[]] | |
| traverse nodes = do Node x xs <- nodes | |
| next <- traverse xs | |
| return (x:next) | |
| maxDepth :: Int -> Forest a -> Forest a | |
| maxDepth n xs | n > 0 = map (\(Node y ys) -> Node y (maxDepth (n-1) ys)) xs | |
| | otherwise = [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment