Skip to content

Instantly share code, notes, and snippets.

@blitzcode
Created March 5, 2016 13:16
Show Gist options
  • Save blitzcode/9d96be6eaea7dfd62742 to your computer and use it in GitHub Desktop.
Save blitzcode/9d96be6eaea7dfd62742 to your computer and use it in GitHub Desktop.
Haskell Bin Tree to List
module Main where
data Tree a = Empty | Node a (Tree a) (Tree a)
tree :: Tree Int
tree = Node 4 (Node 2 (Node 1 Empty Empty) (Node 3 Empty Empty)) (Node 6 (Node 5 Empty Empty) (Node 7 Empty Empty))
toList :: Tree a -> [a]
toList root = go root []
where go :: Tree a -> [a] -> [a]
go Empty xs = xs
go (Node v l r) xs = go l $ v : go r xs
main :: IO ()
main = do
print $ toList tree
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment