Skip to content

Instantly share code, notes, and snippets.

@aristidb
Created October 7, 2011 17:22
Show Gist options
  • Select an option

  • Save aristidb/1270845 to your computer and use it in GitHub Desktop.

Select an option

Save aristidb/1270845 to your computer and use it in GitHub Desktop.
Code to generate list permutations
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