Create a gist now

Instantly share code, notes, and snippets.

@passy /collapse.hs
Last active Feb 3, 2018

Embed
What would you like to do?
data Tree a = Empty | Node a (Tree a) (Tree a)
deriving Show
type IntTree = Tree Int
collapse :: IntTree -> [Int]
collapse t =
case t of
Empty -> [-1]
Node a Empty r -> a : -1 : collapse r
Node a l Empty -> a : collapse l ++ [-1]
Node a l r -> a : collapse l ++ collapse r
main =
-- 1
-- \
-- 2
-- / \
-- 3 4
-- \
-- 5
--
let tree :: Tree Int = Node 1 (Empty) (Node 2 (Node 3 Empty Empty) (Node 4 Empty (Node 5 Empty Empty)))
in
print $ collapse tree
-- Prints: [1,-1,2,3,-1,-1,4,-1,5,-1,-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment