-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio_testing.hs
More file actions
85 lines (72 loc) · 1.5 KB
/
io_testing.hs
File metadata and controls
85 lines (72 loc) · 1.5 KB
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
import Data.Maybe (Maybe)
toList :: String -> [Integer]
toList input = read (" [" ++ input ++ "] " )
maybeRead :: Read a => String -> Maybe a
maybeRead s = case reads s of
[(x, "")] -> Just x
_ -> Nothing
maybeToList :: String -> Maybe [Integer]
maybeToList input = maybeRead $ "[" ++ input ++ "]"
getNumbers :: IO [Integer]
getNumbers = do
putStr "enter a list of numbers separated by commas\n"
input <- getLine
let maybeList = maybeToList input in
case maybeList of
Just l -> return l
Nothing -> do
putStr "Try again\n"
getNumbers
main :: IO()
main = do
list <- getNumbers
print $ sum list
-- do
-- x <- action1
-- y <- action2
-- z <- action3
--
-- can be replaced with
-- action1 >>= (\x ->
-- action2 >>= (\y ->
-- action3 >>= (\z ->
-- ...
--)))
test1 = do
line1 <- getLine
line2 <- getLine
print (line1 ++ line2)
test2 =
getLine >>= (\line1 ->
getLine >>= (\line2 ->
print (line1 ++ line2)))
-- do
-- action1
-- action2
-- action3
--
-- can be replaced with
-- action1 >>
-- action2 >>
-- action3
test3 = do
putStr "ASDF\n"
putStr "ZQWC\n"
test4 =
putStr "ASDF\n" >>
putStr "ZQWC\n"
getNumbers' :: IO [Integer]
getNumbers' =
putStr "enter a list of numbers separated by commas\n" >>
getLine >>= (\input ->
let maybeList = maybeToList input in
case maybeList of
Just l -> return l
Nothing ->
putStr "Try again\n" >>
getNumbers'
)
main' :: IO()
main' =
getNumbers >>=
\list -> print $ sum list