Skip to content

Instantly share code, notes, and snippets.

@bitwombat
Created July 14, 2019 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitwombat/eb841312baf275b2fc0e678b9d6b895b to your computer and use it in GitHub Desktop.
Save bitwombat/eb841312baf275b2fc0e678b9d6b895b to your computer and use it in GitHub Desktop.
mapTree exercise, page 440.
module Page440 where
data BinaryTree a =
Leaf
| Node (BinaryTree a) a (BinaryTree a)
deriving (Eq, Ord, Show)
mapTree :: (a -> b)
-> BinaryTree a
-> BinaryTree b
mapTree _ Leaf = Leaf
mapTree f (Node left a right) =
Node (mapTree f left) (f a) (mapTree f right)
testTree' :: BinaryTree Integer
testTree' =
Node (Node Leaf 3 Leaf)
1
(Node Leaf 4 Leaf)
mapExpected =
Node (Node Leaf 4 Leaf)
2
(Node Leaf 5 Leaf)
-- acceptance test for mapTree
mapOkay =
if mapTree (+1) testTree' == mapExpected
then print "yup okay!"
else error "test failed!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment