-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack8.hs
73 lines (66 loc) · 1.43 KB
/
stack8.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
import Control.Monad.State
-- Stack
newtype Stack a = Stack [a] deriving Show
type StackOp a b = State (Stack a) b
-- Stackの先頭に要素を追加
-- (追加する要素) -> (初期Stack) -> (先端の要素, 更新されたStack)
push :: a -> StackOp a a
push c =
get
>>= \(Stack cs) -> put (Stack (c:cs))
>> return c
-- Stackの専用の要素を取り出す
-- (初期Stack) -> (取り出された要素, 更新されたStack)
pop :: StackOp a a
pop =
get
>>= \(Stack (c:cs)) -> put (Stack cs)
>> return c
-- popした要素を調べる
headIs :: (a -> Bool) -> StackOp a Bool
headIs checker =
pop
>>= \i -> return (checker i)
-- 空にする
empty :: StackOp a ()
empty =
put (Stack [])
>> return ()
main = do
let stack = Stack [1, 2, 3, 4, 5]
print
$ (`runState` stack)
$ headIs (> 4)
>>= \i1 -> headIs (> 3)
>>= \i2 -> headIs (> 2)
>>= \i3 -> return (and [i1, i2, i3])
print
$ (`runState` stack)
$ pop
>>= \i1 -> pop
>>= \i2 -> return (i1 + i2)
print
$ (`runState` stack)
$ push 10
>> push 12
>> pop
>>= \i -> return i
print
$ (`runState` stack)
$ pop
>>= \i -> push 10
>> push 12
>> return i
print
$ (`runState` stack)
$ pop
>>= \i1 -> pop
>>= \i2 -> push 10
>> push 12
>> return (i1 + i2)
print
$ (`runState` stack)
$ push 10
>> empty
>> push 11
>> push 12