Created
July 14, 2019 23:01
-
-
Save bitwombat/eb841312baf275b2fc0e678b9d6b895b to your computer and use it in GitHub Desktop.
mapTree exercise, page 440.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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