Updated haskell.html.markdown with a better IO() tutorial

I have added a more detailed tutorial on IO(). I feel that what was there before would only make a newcomer confused. When I was learning this, the crucial insight was to understand how to interpret the types of the IO functions, and I tried to elucidate this.

There are also three minor, related changes:

I changed the `say` function to be Color -> String. I found the older version Color -> IO String confusing, and it did not compile anyway. I also use the new `say` function.

I added a note that the REPL can run IO actions.

There was a minor error in the `action` function that I fixed.
This commit is contained in:
ilyagr 2013-07-03 17:31:41 -07:00
parent 171b01bb02
commit 90929bfc6d

View File

@ -281,10 +281,11 @@ data Color = Red | Blue | Green
-- Now you can use it in a function:
say :: Color -> IO String
say Red = putStrLn "You are Red!"
say Blue = putStrLn "You are Blue!"
say Green = putStrLn "You are Green!"
say :: Color -> String
say Red = "You are Red!"
say Blue = "You are Blue!"
say Green = "You are Green!"
-- Your data types can have parameters too:
@ -306,7 +307,7 @@ Just 1
-- called. It must return a value of type `IO ()`. For example:
main :: IO ()
main = putStrLn "Hello, sky! " ++ (say Blue)
main = putStrLn $ "Hello, sky! " ++ (say Blue)
-- putStrLn has type String -> IO ()
-- It is easiest to do IO if you can implement your program as
@ -329,9 +330,10 @@ sayHello :: IO ()
sayHello = do
putStrLn "What is your name?"
name <- getLine -- this gets a line and gives it the name "input"
putStrLn "Hello, " ++ name
putStrLn $ "Hello, " ++ name
-- Exercise: write your own version of `interact`.
-- Exercise: write your own version of `interact` that only reads
-- one line of input.
-- The code in `sayHello` will never be executed, however. The only
-- action that ever gets executed is the value of `main`.
@ -351,7 +353,7 @@ sayHello = do
action :: IO String
action = do
putStrLn "This is a line. Duh"
input <- getLine
input1 <- getLine
input2 <- getLine
-- The type of the `do` statement is that of its last line.
-- `return` is not a keyword, but merely a function
@ -389,6 +391,14 @@ let foo = 5
>:t foo
foo :: Integer
-- You can also run any action of type `IO ()`
> sayHello
What is your name?
Friend!
Hello, Friend!
```
There's a lot more to Haskell, including typeclasses and monads. These are the big ideas that make Haskell such fun to code in. I'll leave you with one final Haskell example: an implementation of quicksort in Haskell: