-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.hs
More file actions
57 lines (47 loc) · 1.5 KB
/
input.hs
File metadata and controls
57 lines (47 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
module Input where
readMaybe :: String -> Maybe Integer
readMaybe s = case reads s of
[(x, "")] -> Just x
_ -> Nothing
allGoodInput :: [Maybe t] -> [t]
allGoodInput ((Just a):xs) = a : (allGoodInput xs)
allGoodInput (Nothing:xs) = allGoodInput xs
allGoodInput [] = []
tillGoodInput :: [Maybe t] -> t
tillGoodInput ((Just a):_) = a
tillGoodInput (Nothing:xs) = tillGoodInput xs
tillGoodInput [] = error "Empty List"
nextGoodIntQues :: String -> String -> IO Integer
nextGoodIntQues question badInMessage = do
putStrLn question
nextGoodInput badInMessage
-- keeps asking for input until it is a number
-- The string is the message to display on invalid input.
nextGoodInput :: String -> IO Integer
nextGoodInput badInMessage = do
ln <- getLine
let ini = readMaybe ln
case ini of
Nothing -> do
putStrLn badInMessage
nextGoodInput badInMessage
Just x -> return x
nextGoodInputStdMsg :: IO Integer
nextGoodInputStdMsg = nextGoodInput "That's not a number. Please enter a number."
getAllInput :: IO [Maybe Integer]
getAllInput = do
ln <- getLine
let m = (readMaybe ln :: (Maybe Integer))
remainder <- getAllInput
return (m:remainder)
-- Asks for all the inputs in a row.
takeIn :: Int -> IO [String]
takeIn n = sequence $ take n allInputi
--Infinite
allInput :: IO [String]
allInput = do
ln <-getLine
remainder <- allInput
return (ln : remainder)
allInputi :: [IO String]
allInputi = getLine : allInputi