Skip to content

Instantly share code, notes, and snippets.

@rdbuf
Last active September 17, 2017 20:12
Show Gist options
  • Save rdbuf/abf4060e0021a6dac539bd386e9ebc8d to your computer and use it in GitHub Desktop.
Save rdbuf/abf4060e0021a6dac539bd386e9ebc8d to your computer and use it in GitHub Desktop.
Tree implementations in Haskell
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)
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