-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIOfirst.hs
101 lines (80 loc) · 1.65 KB
/
IOfirst.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
module IOkit where
import Prelude hiding
( putStr
, putStrLn
, getLine
, interact
, (>>)
, (>>=)
)
import System.IO
( hSetEcho
, hGetEcho
, stdin
, stdout
)
getLine :: IO String
-- {{{ spoiler
getLine = do
c <- getChar
if isEOL c
then return ""
else do l <- getLine
return (c:l)
-- }}}
isEOL :: Char -> Bool
-- {{{ spoiler
isEOL = (== '\n')
-- }}}
getInt :: IO Int
-- {{{ spoiler
getInt =
do s <- getLine
return $ read s
-- }}}
{-
-- {{{ spoiler
getInt = iomap read getLine
-- }}}
-}
{- alternative definitions
-- first definition:
-- also valid one-liner:
-- {{{ spoiler
getInt = getLine >>= (return . read)
-- }}}
-}
getSafeInt :: IO (Maybe Int)
-- {{{ spoiler
getSafeInt =
do s <- getLine
let parsed = reads s :: [(Int, String)]
case parsed of
[(n, "")] -> return $ Just n
_ -> return Nothing
-- }}}
-- sequencing: first do f ignoring its result, then do g and keep its result
infixl 1 >>
(>>) :: IO a -> IO b -> IO b
(>>) = undefined
-- transforms the action given to an equivalent one that has no result
void :: IO a -> IO ()
void = undefined
-- pauses till the user presses any normal key
pause :: IO ()
pause = undefined
skip :: IO ()
skip = undefined
newline :: IO ()
newline = undefined
-- exercise: define it as a foldr
putStr :: String -> IO ()
putStr = undefined
-- transform f into one "just like f" except that it prints a newline
-- after any side-effects f may had
lnize :: (a -> IO b) -> a -> IO b
lnize = undefined
putStrLn :: String -> IO ()
putStrLn = undefined
putCharLn :: Char -> IO ()
putCharLn = undefined