Skip to content

Instantly share code, notes, and snippets.

@bananu7
Created May 23, 2017 14:39
Show Gist options
  • Select an option

  • Save bananu7/88190fcbe5af0954e31be2a4dc1c520d to your computer and use it in GitHub Desktop.

Select an option

Save bananu7/88190fcbe5af0954e31be2a4dc1c520d to your computer and use it in GitHub Desktop.
data Tree =
NodeLeaf Int |
NodeA Tree |
NodeB Tree
processTree :: Tree -> IO ()
processTree (NodeLeaf a) = putStr . show $ a
processTree (NodeA t) = putStr "a-" >> processTree t
processTree (NodeB t) = putStr "b-" >> processTree t
-- Simply add P
type P = Int
data TreeP =
NodeLeafP Int P |
NodeAP TreeP P |
NodeBP TreeP P
processTreeP :: TreeP -> IO ()
processTreeP (NodeLeafP a p) = putStrLn $ show a ++ ":" ++ show p
processTreeP (NodeAP t p) = (putStr $ "a:" ++ show p ++ "-") >> processTreeP t
processTreeP (NodeBP t p) = (putStr $ "b:" ++ show p ++ "-") >> processTreeP t
-- create wrapped nodes
data WithP a = WithP a P
data TreeW =
NodeLeafW (WithP Int) |
NodeAW (WithP TreeW) |
NodeBW (WithP TreeW)
main = do
processTree (NodeA (NodeB (NodeLeaf 5)))
putStrLn ""
processTreeP (NodeAP (NodeBP (NodeLeafP 5 3) 2) 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment