Загрузил EruFilosifoMakar

# 5 Monady

Реклама
```5.1 Класс типов Functor и законы для него
1. instance Functor Point3D where
fmap f (Point3D a b c) = Point3D (f a) (f b) (f c)
2. instance Functor GeomPrimitive where
fmap f (Point x) = Point (fmap f x)
fmap f (LineSegment x y) = LineSegment (fmap f x) (fmap f y)
3. instance Functor Tree where
fmap f (Leaf x) = Leaf (fmap f x)
fmap f (Branch l x r) = Branch (fmap f l) (fmap f x) (fmap f r)
4. instance Functor (Entry k1 k2) where
fmap f (Entry (k1, k2) v) = Entry (k1, k2) (f v)
instance Functor (Map k1 k2) where
fmap f (Map entries) = Map (map (fmap f) entries)
5.2 Определение монады
1. toLogger :: (a -&gt; b) -&gt; String -&gt; (a -&gt; Log b)
toLogger f msg = Log [msg] . f
execLoggers :: a -&gt; (a -&gt; Log b) -&gt; (b -&gt; Log c) -&gt; Log c
execLoggers a f g = Log (logb ++ logc) c where
(Log logb b) = f a
(Log logc c) = g b
returnLog :: a -&gt; Log a
returnLog = Log []
2. toLogger :: (a -&gt; b) -&gt; String -&gt; (a -&gt; Log b)
toLogger f msg = Log [msg] . f
execLoggers :: a -&gt; (a -&gt; Log b) -&gt; (b -&gt; Log c) -&gt; Log c
execLoggers a f g = Log (logb ++ logc) c where
(Log logb b) = f a
(Log logc c) = g b
returnLog :: a -&gt; Log a
returnLog = Log []
bindLog :: Log a -&gt; (a -&gt; Log b) -&gt; Log b
bindLog (Log msga a) f = Log (msga ++ msgb) b where
(Log msgb b) = f a
3. хз
4. хз
5. хз
5.3 Монада Identity
1. хз
2. Не выполняется первый закон
Не выполняется второй закон
3. Все законы выполняются
4.
Не выполняется 1-й закон Не выполняется 3-й закон
5.4 Список и Maybe как монады
1.
import Data.Char (isDigit)
asToken :: String -&gt; Maybe Token
asToken x = case x of
[] -&gt; Nothing
&quot;(&quot; -&gt; Just LeftBrace
&quot;)&quot; -&gt; Just RightBrace
&quot;-&quot; -&gt; Just Minus
&quot;+&quot; -&gt; Just Plus
_ | all isDigit x -&gt; Just \$ Number \$ read x
| otherwise -&gt; Nothing
tokenize :: String -&gt; Maybe [Token]
tokenize = sequence . (map asToken) . words
2.
nextPositionsN :: Board -&gt; Int -&gt; (Board -&gt; Bool) -&gt; [Board]
nextPositionsN b n pred
| n &lt; 0 = []
| n == 0 = filter pred [b]
| otherwise = do
move &lt;- nextPositions b
restMoves &lt;- nextPositionsN move (n - 1) pred
return restMoves
3.
pythagoreanTriple :: Int -&gt; [(Int, Int, Int)]
pythagoreanTriple x
| x &lt;= 0 = []
| otherwise = do
b &lt;- [1..x]
a &lt;- [1..b-1]
c &lt;- [1..x]
True &lt;- return \$ (a^2 + b^2) == c^2
return (a,b,c)
5.5 Монада IO
1.
main' :: IO ()
main' = do
putStr \$ &quot;What is your name?\nName: &quot;
name &lt;- getLine
if null name
then main'
else putStrLn \$ &quot;Hi, &quot; ++ name ++ &quot;!&quot;
2.
import Data.List (isInfixOf, filter)
import Control.Monad(liftM)
main' :: IO ()
main' = do
putStr \$ &quot;Substring: &quot;
pattern &lt;- getLine
if null pattern
then putStrLn &quot;Canceled&quot;
else getFiles pattern &gt;&gt;= mapM_ deleteFile
getFiles :: String -&gt; IO [FilePath]
getFiles pattern =
liftM (filter (isInfixOf pattern)) \$ getDirectoryContents &quot;.&quot;
deleteFile :: FilePath -&gt; IO ()
deleteFile path = do
putStrLn \$ &quot;Removing file: &quot; ++ path
removeFile path
5.6 Монада Reader
1. 24
2. В сочетании с монадой Reader этот оператор бесполезен
3.
local' :: (r -&gt; e) -&gt; Reader e a -&gt; Reader r a
4.
import Control.Monad (ap, liftM)
local' :: (r -&gt; r') -&gt; Reader r' a -&gt; Reader r a
local' f m = Reader \$ (runReader m) . f
5.
usersWithBadPasswords :: Reader UsersTable [User]
usersWithBadPasswords = asks \$ map fst . filter isBad where
isBad = (&quot;123456&quot; ==) . snd
5.7 Монада Writer
1.
evalWriter :: Writer w a -&gt; a
evalWriter = fst . runWriter
2.хз
3.
purchase :: String -&gt; Integer -&gt; Shopping
purchase _ cost = writer ((), Sum cost)
total :: Shopping -&gt; Integer
total = getSum . execWriter
4.
type Shopping = Writer ([(String, Integer)]) ()
purchase :: String -&gt; Integer -&gt; Shopping
purchase item price = writer ((), [(item, price)])
total :: Shopping -&gt; Integer
total = sum . (map snd) . execWriter
items :: Shopping -&gt; [String]
items = (map fst) . execWriter
5.8 Монада State
1.
Монада Reader является частным случаем монады State
Монада Writer является частным случаем монады State
2. Монада State реализована в одном из пакетов Haskell Platform
3.
readerToState :: Reader r a -&gt; State r a
readerToState m = state \$ \e -&gt; (runReader m e, e)
4.
writerToState :: Monoid w =&gt; Writer w a -&gt; State w a
writerToState m = let
(a, w) = runWriter m
in state \$ \e -&gt; (a, e `mappend` w)
5.
fibStep :: State (Integer, Integer) ()
fibStep = do
(a, b) &lt;- get
put (b, a + b)
execStateN :: Int -&gt; State s a -&gt; s -&gt; s
execStateN n m = execState (replicateM n m)
6. хз
```