-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChapter9.hs
45 lines (31 loc) · 983 Bytes
/
Chapter9.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
module Chapter9 where
{-
Interactive Programs (IO monad)
to interact with the outside world (with side effects)
In the early days, Haskell cant deal with IO
Input --> Program --> Output
getChar :: IO Char
putChar :: Char -> IO ()
return :: a -> IO a
getLine :: IO String
getLine = do x <- getChar
if x == '\n'
then return []
else do xs <- getLine
return (x : xs)
putStr :: String -> IO ()
putStr [] = return ()
putStr (x : xs) = do putChar x
putStr xs
putStrLn :: String -> IO ()
putStrLn xs = do putStr x
putChar '\n'
-}
strLen :: IO ()
strLen = do putStr "Enter a string: "
xs <- getLine
putStrLn ("The string has " ++ (show (length xs)) ++ " characters")
strLen' :: IO ()
strLen' = putStr "Enter a string: " >>=
(\ _ -> getLine >>=
(\ xs -> putStrLn ("The string has " ++ (show (length xs)) ++ " characters")))