-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Anastasios Valtinos
committed
Nov 1, 2022
1 parent
7d2eee0
commit 165941f
Showing
2 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
Haskell Excercises & Code/Alejandro - Book of Monads/5c6d7d7b-02fa-4e67-b84b-aac2ee3b3db7.hs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{-# LANGUAGE FlexibleContexts #-} | ||
module MonadicCode where | ||
|
||
import Control.Monad.State as CMS | ||
|
||
|
||
-- implement `ap` yourself | ||
|
||
ap'' :: Monad m => m (a -> b) -> m a -> m b | ||
ap'' mf mx = do | ||
f <- mf | ||
x <- mx | ||
return (f x) | ||
|
||
|
||
-- Another exercise. Write fmap with ap & pure | ||
|
||
-- fmap'' :: Functor f => (a -> b) -> f a -> f b | ||
fmap'' f a = pure f `ap` a | ||
|
||
-- Ex 3.3 Write ZipList functor instancce | ||
|
||
newtype ZipList a = ZipList { getZipList :: [a] } deriving (Eq, Show) | ||
-- so getZipList type :: ZipList a -> [a] | ||
|
||
instance Functor ZipList where | ||
fmap f (ZipList gz) = ZipList $ fmap f gz | ||
|
||
newtype State'' s a = State { runState :: s -> (a,s) } | ||
|
||
-- runState :: State s a -> s -> (a,s) | ||
|
||
instance Functor (State'' s) where | ||
fmap f (State rs) = State $ \s -> let (a,s') = rs s in (f a, s') | ||
|
||
instance Applicative (State'' s) where | ||
-- pure :: x -> State s x | ||
pure x = State $ \n -> (x, n) | ||
State fz <*> State xz = State $ \s -> let a = fst $ xz s | ||
ab = fst $ fz s | ||
in (ab a, s) |
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