-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSet11a.hs
183 lines (159 loc) · 4.65 KB
/
Set11a.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
module Set11a where
import Control.Monad
import Data.List
import Mooc.Todo
import System.IO
-- Lecture 11:
-- * The IO type
-- * do-notation
--
-- Useful functions / operations:
-- * putStrLn
-- * getLine
-- * readLn
-- * replicateM
-- * readFile
-- * lines
------------------------------------------------------------------------------
-- Ex 1: define an IO operation hello that prints two lines. The
-- first line should be HELLO and the second one WORLD
hello :: IO ()
hello = do
putStrLn "HELLO"
putStrLn "WORLD"
------------------------------------------------------------------------------
-- Ex 2: define the IO operation greet that takes a name as an
-- argument and prints a line "HELLO name".
greet :: String -> IO ()
greet name = do
putStrLn ("HELLO " ++ name)
------------------------------------------------------------------------------
-- Ex 3: define the IO operation greet2 that reads a name from the
-- keyboard and then greets that name like the in the previous
-- exercise.
--
-- Try to use the greet operation in your solution.
greet2 :: IO ()
greet2 = do
s <- getLine
greet s
------------------------------------------------------------------------------
-- Ex 4: define the IO operation readWords n which reads n lines from
-- the user and produces them as a list, in alphabetical order.
--
-- Example in GHCi:
-- Set11> readWords 3
-- bob
-- alice
-- carl
-- ["alice","bob","carl"]
readWords :: Int -> IO [String]
readWords 0 = return []
readWords n = do
i <- getLine
xs <- readWords (n -1)
return $ sort (i : xs)
------------------------------------------------------------------------------
-- Ex 5: define the IO operation readUntil f, which reads lines from
-- the user and returns them as a list. Reading is stopped when f
-- returns True for a line. (The value for which f returns True is not
-- returned.)
--
-- Example in GHCi:
-- *Set11> readUntil (=="STOP")
-- bananas
-- garlic
-- pakchoi
-- STOP
-- ["bananas","garlic","pakchoi"]
readUntil :: (String -> Bool) -> IO [String]
readUntil f = do
go []
where
go a = do
i <- getLine
let val = f i
if not val then go (a ++ [i]) else return a
-- if val then return [] else readUntil f
------------------------------------------------------------------------------
-- Ex 6: given n, print the numbers from n to 0, one per line
countdownPrint :: Int -> IO ()
countdownPrint 0 = do
print 0
return ()
countdownPrint n = do
print n
countdownPrint $ n - 1
------------------------------------------------------------------------------
-- Ex 7: isums n should read n numbers from the user (one per line) and
-- 1) after each number, print the running sum up to that number
-- 2) finally, produce the sum of all numbers
--
-- Example:
-- 1. run `isums 3`
-- 2. user enters '3', should print '3'
-- 3. user enters '5', should print '8' (3+5)
-- 4. user enters '1', should print '9' (3+5+1)
-- 5. produces 9
isums :: Int -> IO Int
isums n = do
go n 0
where
go 0 a = do
return a
go n a = do
i <- readLn
print $ a + i
go (n - 1) (a + i)
------------------------------------------------------------------------------
-- Ex 8: when is a useful function, but its first argument has type
-- Bool. Write a function that behaves similarly but the first
-- argument has type IO Bool.
whenM :: IO Bool -> IO () -> IO ()
whenM cond op = do
val <- cond
when val op
------------------------------------------------------------------------------
-- Ex 9: implement the while loop. while condition operation should
-- run operation as long as condition returns True.
--
-- Examples:
-- -- prints nothing
-- while (return False) (putStrLn "IMPOSSIBLE")
--
-- -- prints YAY! as long as the user keeps answering Y
-- while ask (putStrLn "YAY!")
-- used in an example
ask :: IO Bool
ask = do
putStrLn "Y/N?"
line <- getLine
return $ line == "Y"
while :: IO Bool -> IO () -> IO ()
while cond op = do
val <- cond
when val op
-- print "YAY!"
when val $ while cond op
------------------------------------------------------------------------------
-- Ex 10: given a string and an IO operation, print the string, run
-- the IO operation, print the string again, and finally return what
-- the operation returned.
--
-- Note! the operation should be run only once
--
-- Examples:
-- debug "CIAO" (return 3)
-- - prints two lines that contain CIAO
-- - returns the value 3
-- debug "BOOM" getLine
-- 1. prints "BOOM"
-- 2. reads a line from the user
-- 3. prints "BOOM"
-- 4. returns the line read from the user
debug :: String -> IO a -> IO a
debug s op = do
putStrLn s
i <- op
putStrLn s
return i