-
Notifications
You must be signed in to change notification settings - Fork 1
/
reverseSentence.hs
42 lines (31 loc) · 1.02 KB
/
reverseSentence.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
-- Part of 09_input_and_output.hs (but independent on its own)
-- Learn You a Haskell For Great Good
-- Chapter 9: Input and Output
-- http://learnyouahaskell.com/input-and-output
-- Two ways of executing this haskell file
-- From Command line:
-- Method 1: Compile into an "exe" and run
-- > ghc --make reverseSentence.hs
-- > ./reverseSentence.exe
-- Method 2: Run from commandline without creating "exe"
-- > runhaskell reverseSentence.hs
main = do
line <- getLine
if null line
then return ()
else do
putStrLn $ reverseWords line
main
reverseWords :: String -> String
reverseWords = unwords . map reverse . words
-- The following two are the same!
-- return takes in a value and bind it to IO
-- "<-" operator takes in a IO String and gets the value
main' = do
a <- return "hell"
b <- return "yeah!"
putStrLn $ a ++ " " ++ b
main'' = do
let a = "hell"
b = "yeah"
putStrLn $ a ++ " " ++ b