-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMonadTutorial.hs
More file actions
33 lines (24 loc) · 773 Bytes
/
Copy pathMonadTutorial.hs
File metadata and controls
33 lines (24 loc) · 773 Bytes
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
module MonadTutorial
where
import Control.Monad
import Control.Monad.State
-- example 1
type Sheep = String
father :: Sheep -> Maybe Sheep
father s= Just $ "dad of " ++ s
mother :: Sheep -> Maybe Sheep
mother s= Just $ "mum of " ++ s
test :: String -> Maybe Int
test _=Just 0
type ParserT a = StateT String Maybe a
type Parser a=State String a
s1=state (\st->(length st,st))
psMult2 i=state $ (,) (2 * i)
s2=get >>= \v-> s1 >>= psMult2 >>= \w-> put (v++"adios") >> return w
t1=runState s2 "hola"
-- (8,"holaadios")
t2=runState (s2>>s2) "hola"
-- (18,"holaadiosadios")
-- If your monad has a *terminator* element such that
-- *terminator >>= f = terminator*, then you can use >>= recursively
badmany p = p >>= \x-> badmany p >>= \xs-> return (x:xs)