Lesson 21. Hello World!—introducing IO types
After reading lesson 21, you’ll be able to
- Understand how Haskell handles I/O by using IO types
- Use do-notation to perform I/O
- Write pure programs that interact with the real world
In lesson 1, you saw a basic example of a Hello World program. In this lesson, you’ll revisit a similar program to get a better sense of how I/O works in Haskell. Here’s an example program using I/O that reads a name from the command line and prints out "Hello <name>!".
Before you saw any Haskell, you likely could have read this program pretty well. Unfortunately, now that you know more about Haskell, this probably looks much more confusing! The helloPerson function should be straightforward, but everything starting with main is different from anything else you’ve seen so far. You should have the following questions:
- What in the world is the type IO ()?
- Why is there a do after main?
- Does putStrLn return a value?
- Why are some variables assigned with <- and others with let?
By the end of this lesson, you’ll have a reasonable explanation of each of these things, and hopefully a much better understanding of the basics of IO in Haskell.