Skip to content

Commit

Permalink
completed the execises on Course.Monad
Browse files Browse the repository at this point in the history
  • Loading branch information
miladz68 committed Dec 18, 2016
1 parent 555037f commit cdf229f
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions src/Course/Monad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
module Course.Monad(
Monad(..)
, join
, (>>=)
, (>>=)
, (<=<)
) where

Expand Down Expand Up @@ -68,8 +68,7 @@ infixr 1 =<<
f (a -> b)
-> f a
-> f b
(<*>) =
error "todo: Course.Monad#(<*>)"
(<*>) ff fa = (=<<) (<$> fa) ff

infixl 4 <*>

Expand All @@ -82,8 +81,7 @@ instance Monad Id where
(a -> Id b)
-> Id a
-> Id b
(=<<) =
error "todo: Course.Monad (=<<)#instance Id"
(=<<) f (Id a) = f a

-- | Binds a function on a List.
--
Expand All @@ -94,8 +92,9 @@ instance Monad List where
(a -> List b)
-> List a
-> List b
(=<<) =
error "todo: Course.Monad (=<<)#instance List"
(=<<) _ Nil = Nil
(=<<) f (a:.Nil) = f a
(=<<) f (a:.as) = f a ++ (=<<) f as

-- | Binds a function on an Optional.
--
Expand All @@ -106,20 +105,18 @@ instance Monad Optional where
(a -> Optional b)
-> Optional a
-> Optional b
(=<<) =
error "todo: Course.Monad (=<<)#instance Optional"

(=<<) f (Full a) = f a
(=<<) _ Empty = Empty
-- | Binds a function on the reader ((->) t).
--
-- >>> ((*) =<< (+10)) 7
-- 119
instance Monad ((->) t) where
(=<<) ::
(a -> ((->) t b))
-> ((->) t a)
-> ((->) t b)
(=<<) =
error "todo: Course.Monad (=<<)#instance ((->) t)"
(a -> (->) t b)
-> (->) t a
-> (->) t b
(f =<< g) t = f (g t) t

-- | Flattens a combined structure to a single structure.
--
Expand All @@ -138,8 +135,7 @@ join ::
Monad f =>
f (f a)
-> f a
join =
error "todo: Course.Monad#join"
join = (=<<) id

-- | Implement a flipped version of @(=<<)@, however, use only
-- @join@ and @(<$>)@.
Expand All @@ -152,8 +148,7 @@ join =
f a
-> (a -> f b)
-> f b
(>>=) =
error "todo: Course.Monad#(>>=)"
(>>=) fa ff = (=<<) ff fa

infixl 1 >>=

Expand All @@ -168,8 +163,7 @@ infixl 1 >>=
-> (a -> f b)
-> a
-> f c
(<=<) =
error "todo: Course.Monad#(<=<)"
(<=<) bfc afb a = (=<<) bfc (afb a)

infixr 1 <=<

Expand Down

0 comments on commit cdf229f

Please sign in to comment.