-
Notifications
You must be signed in to change notification settings - Fork 0
/
Examples.hs
96 lines (60 loc) · 2.01 KB
/
Examples.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
86
87
88
89
90
91
92
93
94
95
96
module Examples where
import Interaction
readWrite :: Interact () ()
readWrite = readin `sq` writeout id
copy :: Interact () ()
copy = readin `sq` writeout id `sq` copy
test1 :: IO ()
test1 = run readWrite ()
test2 :: IO ()
test2 = runL readWrite ()
readInt :: String -> Int
readInt = read
getInt :: Interact () Int
getInt = readin `sq` apply read
addNum :: Interact Int Int
addNum = readI ((+).readInt) `sq` showkeep
addNums :: Interact Int Int
addNums
= while (not.eof)
addNum
addNumsToZero :: Interact Int Int
addNumsToZero
= while (not.isZero)
addNum
where isZero (_,st) = st==0
collectNums :: Interact Int Int
collectNums
= addNum `pass_param` (\n -> start 0 `sq`
seqlist (replicate n addNum) `sq`
write "finished")
collector :: Interact () (Int,Int)
collector
= getInt `sq` -- state is counter
add_val_right 0 `sq` -- now is (counter,sum)
while ((>(0::Int)).fst.snd) -- still is (counter,sum)
(add_val_left () `sq` -- now ((),(counter,sum))
pass_on getInt `sq` -- now (Int,(counter,sum))
apply (\(p,(m,s))->(m-1,s+p)) `sq` -- is again (counter,sum)
wait `sq` -- see what happens when this is commented out :-)
showkeep)
{-
Getting the irrefutable patterns right - messy
-}
oneWord :: Interact () ()
oneWord = writeln "word"
manyWords :: Interact () ()
manyWords = oneWord `sq` manyWords
moreWords :: Interact () ()
moreWords x
= make_Output ["word"] (moreWords x)
evenmoreWords :: Interact () ()
evenmoreWords x
= (y,z,["word"]++out)
where
(y,z,out) = evenmoreWords x
-- Flavours of echo
necho ys
= "Prompt: " ++ [head ys] ++ "\n" ++ necho (tail ys)
echon (x:xs)
= "Prompt: " ++ [x] ++ "\n" ++ echon xs