forked from cyga/real-world-haskell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Do.hs
85 lines (74 loc) · 1.32 KB
/
Do.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
-- file: ch14/Do.hs
doNotation1 =
do act
-- file: ch14/Do.hs
translated1 =
act
-- file: ch14/Do.hs
doNotation2 =
do act1
act2
{- ... etc. -}
actN
-- file: ch14/Do.hs
translated2 =
act1 >>
do act2
{- ... etc. -}
actN
finalTranslation2 =
act1 >>
act2 >>
{- ... etc. -}
actN
-- file: ch14/Do.hs
doNotation3 =
do pattern <- act1
act2
{- ... etc. -}
actN
-- file: ch14/Do.hs
translated3 =
let f pattern = do act2
{- ... etc. -}
actN
f _ = fail "..."
in act1 >>= f
-- file: ch14/Do.hs
robust :: [a] -> Maybe a
robust xs = do (_:x:_) <- Just xs
return x
-- file: ch14/Do.hs
doNotation4 =
do let val1 = expr1
val2 = expr2
{- ... etc. -}
valN = exprN
act1
act2
{- ... etc. -}
actN
-- file: ch14/Do.hs
translated4 =
let val1 = expr1
val2 = expr2
valN = exprN
in do act1
act2
{- ... etc. -}
actN
-- file: ch14/Do.hs
semicolon = do
{
act1;
val1 <- act2;
let { val2 = expr1 };
actN;
}
-- file: ch14/Do.hs
semicolonTranslated =
act1 >>
let f val1 = let val2 = expr1
in actN
f _ = fail "..."
in act2 >>= f