-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass.hs
38 lines (27 loc) · 902 Bytes
/
class.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{-# LANGUAGE DeriveFunctor #-}
import Control.Monad ((<=<))
-- Stolen form - <https://adit.io/posts/2013-06-10-three-useful-monads.html>
newtype Writer m a = Writer
{ runWriter :: (a, m)
}
deriving (Functor)
instance (Monoid m) => Applicative (Writer m) where
pure a = Writer (a, mempty)
(Writer (fa, fm)) <*> (Writer (a, am)) =
Writer (fa a, fm <> am)
instance (Monoid m) => Monad (Writer m) where
(Writer (a, am)) >>= func =
let (Writer (b, bm)) = func a in Writer (b, am <> bm)
tell :: String -> Writer String ()
tell str = Writer ((), str)
twice :: (Num a, Show a) => a -> Writer String a
twice a = do
tell ("I twiced " ++ show a ++ "\n")
return (a * a)
quadruple :: Integer -> Writer String Integer
quadruple = twice <=< twice
interleave :: [a] -> [a] -> [a]
interleave (x : xs) ys = x : interleave ys xs
cartesian = foldl1 interleave
main :: a
main = undefined