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
data Tree a = Node a [Tree a] | Leaf a deriving (Show, Eq) | |
sample = Node 1 [Node 2 [Leaf 5, Leaf 6], Node 3 [Leaf 7], Node 4 [Leaf 8]] |
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
reduce f (Leaf x) = x | |
reduce f (Node x xs) = f x . foldr1 f $ map (reduce f) xs | |
addTree = reduce (+) | |
multTree = reduce (*) |
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
class (Functor w) => Comonad w where | |
coreturn :: w a -> a | |
cojoin :: w a -> w (w a) | |
(=>>) :: w a -> (w a -> b) -> w b |
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
instance Functor Tree where | |
fmap f (Leaf a) = Leaf $ f a | |
fmap f (Node a b) = Node (f a) (map (fmap f) b) | |
instance Comonad Tree where | |
coreturn (Leaf a) = a | |
coreturn (Node a _) = a | |
cojoin l@(Leaf a) = Leaf l | |
cojoin n@(Node a b) = Node n (map cojoin b) | |
x =>> f = fmap f $ cojoin x |
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
sums = sample =>> addTree |
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
class Monad m where | |
return :: a -> m a | |
(>>=) :: m a -> (a -> m b) -> m b |
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
data Identity a = Identity a deriving Show | |
instance Monad Identity where | |
return = Identity | |
(Identity a) >>= f = f a | |
m1 = Identity 3 | |
addThree x = return $ x+3 | |
bindExample1 = m1 >>= addThree |
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
instance Monad Maybe where | |
return = Just | |
Nothing >>= f = Nothing | |
(Just a) >>= f = f a |
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
instance Monad [] where | |
return x = [x] | |
xs >>= f = concatMap f xs |
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
instance Monad ((->) r) where | |
return = const | |
{-Note: | |
(>>=) :: (r -> a) -> (a -> (r -> b)) -> (r -> b) | |
f :: (r -> a) | |
g :: (a -> (r -> b)) | |
-} | |
f >>= g = \r -> g (f r) r | |
-- using >>= and lambda functions |