Last active
September 17, 2017 20:12
-
-
Save rdbuf/abf4060e0021a6dac539bd386e9ebc8d to your computer and use it in GitHub Desktop.
Tree implementations in Haskell
This file contains 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
import Data.Maybe | |
import Data.Functor | |
data Tree a = Node a (Maybe (Tree a)) (Maybe (Tree a)) | |
deriving Show | |
instance Functor Tree where | |
fmap f (Node x left right) = Node (f x) (left >>= return . fmap f) (right >>= return . fmap f) | |
main = print $ fmap (2*) (Node 2 (Just $ Node 10 Nothing Nothing) Nothing) |
This file contains 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
import Data.Maybe | |
import Data.Functor | |
data Tree a = Tree a [Tree a] | |
deriving Show | |
instance Functor Tree where | |
fmap f (Tree x children) = Tree (f x) (fmap (fmap f) children) | |
main :: IO () | |
main = print $ fmap (2*) (Tree 1 [Tree 3 [Tree 4 [], Tree 5 []]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment