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
| shortest :: (Num a, Ord a) => Tree a -> a | |
| shortest (Leaf x) = x | |
| shortest (Node x xs) = x + (minimum $ map shortest 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 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
| data Tree a = Node a [Tree a] | Leaf a deriving (Show, Eq) | |
| instance Functor Tree where | |
| fmap f (Leaf a) = Leaf $ f a | |
| fmap f (Node a b) = Node (f a) (map (fmap f) 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
| 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 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 |
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 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
| 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
| 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
| sums = sample =>> addTree |