Skip to content

Instantly share code, notes, and snippets.

@amalloy
Created November 4, 2012 00:47
Show Gist options
  • Save amalloy/4009630 to your computer and use it in GitHub Desktop.
Save amalloy/4009630 to your computer and use it in GitHub Desktop.
treeSeq :: (a -> [a]) -> a -> [a]
treeSeq children root = root : concatMap (treeSeq children) (children root)
data Tree a = Leaf a
| Branch (Tree a) (Tree a)
deriving (Show, Eq)
children :: Tree a -> [Tree a]
children (Leaf a) = []
children (Branch x y) = [x, y]
-- treeSeq children $ Branch (Leaf 1) (Branch (Leaf 2) (Leaf 3))
-- [Branch (Leaf 1) (Branch (Leaf 2) (Leaf 3)),
-- Leaf 1, Branch (Leaf 2) (Leaf 3), Leaf 2, Leaf 3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment