Haskell/Simple input and output
Back to the real world
Beyond internally calculating values, we want our programs to interact with the world. The most common beginners' program in any language simply displays a "hello world" greeting on the screen. Here's a Haskell version:
Prelude> putStrLn "Hello, World!"
putStrLn is one of the standard Prelude tools. As the "putStr" part of the name suggests, it takes a
String as an argument and prints it to the screen. We could use
putStr on its own, but we usually include the "Ln" part so to also print a line break. Thus, whatever else is printed next will appear on a new line.
So now you should be thinking, "what is the type of the putStrLn function?" It takes a
String and gives… um… what? What do we call that? The program doesn't get something back that it can use in another function. Instead, the result involves having the computer change the screen. In other words, it does something in the world outside of the program. What type could that have? Let's see what GHCi tells us:
Prelude> :t putStrLn putStrLn :: String -> IO ()
"IO" stands for "input and output". Wherever there is
IO in a type, interaction with the world outside the program is involved. We'll call these
IO values actions. The other part of the
IO type, in this case
(), is the type of the return value of the action; that is, the type of what it gives back to the program (as opposed to what it does outside the program).
() (pronounced as "unit") is a type that only contains one value also called
() (effectively a tuple with zero elements). Since
putStrLn sends output to the world but doesn't return anything to the program,
() is used as a placeholder. We might read
IO () as "action which returns
A few more examples of when we use IO:
- print a string to the screen
- read a string from a keyboard
- write data to a file
- read data from a file
What makes IO actually work? Lots of things happen behind the scenes to take us from
putStrLn to pixels in the screen, but we don't need to understand any of the details to write our programs. A complete Haskell program is actually a big IO action. In a compiled program, this action is called
main and has type
IO (). From this point of view, to write a Haskell program is to combine actions and functions to form the overall action
main that will be executed when the program is run. The compiler takes care of instructing the computer on how to do this.
|Back in the Type Basics chapter, we mentioned that the type of the
Sequencing actions with do
do notation provides a convenient means of putting actions together (which is essential in doing useful things with Haskell). Consider the following program:
Example: What is your name?
main = do putStrLn "Please enter your name:" name <- getLine putStrLn ("Hello, " ++ name ++ ", how are you?")
Before we get into how do works, take a look at
getLine. It goes to the outside world (to the terminal in this case) and brings back a
String. What is its type?
Prelude> :t getLine getLine :: IO String
getLine is an IO action that, when run, will return a
String. But what about the input? While functions have types like
a -> b which reflect that they take arguments and give back results,
getLine doesn't actually take an argument. It takes as input whatever is in the line in the terminal. However, that line in the outside world isn't a defined value with a type until we bring it into the Haskell program.
The program doesn't know the state of the outside world until runtime, so it cannot predict the exact results of IO actions. To manage the relationship of these IO actions to other aspects of a program, the actions must be executed in a predictable sequence defined in advance in the code. With regular functions that do not perform IO, the exact sequencing of execution is less of an issue — as long as the results eventually go to the right places.
In our name program, we're sequencing three actions: a
putStrLn with a greeting, a
getLine, and another
putStrLn. With the
getLine, we use
<- notation which assigns a variable name to stand for the returned value. We cannot know what the value will be in advance, but we know it will use the specified variable name, so we can then use the variable elsewhere (in this case, to prepare the final message being printed). The final action defines the type of the whole
do block. Here, the final action is the result of a
putStrLn, and so our whole program has type
Write a program which asks the user for the base and height of a right angled triangle, calculates its area, and prints it to the screen. The interaction should look something like:
The base? 3.3 The height? 5.4 The area of that triangle is 8.91You will need to use the function
Left arrow clarifications
While actions like
getLine are almost always used to get values, we are not obliged to actually get them. For example, we could write something like this:
main = do putStrLn "Please enter your name:" getLine putStrLn "Hello, how are you?"
In this case, we don't use the input at all, but we still give the user the experience of entering their name. By omitting the
<-, the action will happen, but the data won't be stored or accessible to the program.
<- can be used with any action except the last
There are very few restrictions on which actions can have values obtained from them. Consider the following example where we put the results of each action into a variable (except the last... more on that later):
Example: putting all results into a variable
main = do x <- putStrLn "Please enter your name:" name <- getLine putStrLn ("Hello, " ++ name ++ ", how are you?")
x gets the value out of its action, but that isn't useful in this case because the action returns the unit value
(). So while we could technically get the value out of any action, it isn't always worth it.
So, what about the final action? Why can't we get a value out of that? Let's see what happens when we try:
Example: getting the value out of the last action
main = do x <- putStrLn "Please enter your name:" name <- getLine y <- putStrLn ("Hello, " ++ name ++ ", how are you?")
HaskellWikibook.hs:5:2: The last statement in a 'do' construct must be an expression
Making sense of this requires a somewhat deeper understanding of Haskell than we currently have. Suffice it to say, whenever you use
<- to get the value of an action, Haskell is always expecting another action to follow it. So the final action cannot have any
Normal Haskell constructions like if/then/else can be used within the do notation, but you need to take some care here. For instance, in a simple "guess the number" program, we have:
doGuessing num = do putStrLn "Enter your guess:" guess <- getLine if (read guess) < num then do putStrLn "Too low!" doGuessing num else if (read guess) > num then do putStrLn "Too high!" doGuessing num else putStrLn "You Win!"
Remember that the if/then/else construction takes three arguments: the condition, the "then" branch, and the "else" branch. The condition needs to have type
Bool, and the two branches can have any type, provided that they have the same type. The type of the entire if/then/else construction is then the type of the two branches.
In the outermost comparison, we have
(read guess) < num as the condition. That has the correct type. Let's now consider the "then" branch. The code here is:
do putStrLn "Too low!" doGuessing num
Here, we are sequencing two actions:
doGuessing. The first has type
IO (), which is fine. The second also has type
IO (), which is fine. The type result of the entire computation is precisely the type of the final computation. Thus, the type of the "then" branch is also
IO (). A similar argument shows that the type of the "else" branch is also
IO (). This means the type of the entire if/then/else construction is
IO (), which is what we want.
Note: be careful if you find yourself thinking, "Well, I already started a do block; I don't need another one." We can't have code like:
do if (read guess) < num then putStrLn "Too low!" doGuessing num else ...
Here, since we didn't repeat the do, the compiler doesn't know that the
doGuessing calls are supposed to be sequenced, and the compiler will think you're trying to call
putStrLn with three arguments: the string, the function
doGuessing and the integer
num, and thus reject the program.
Write a program that asks the user for his or her name. If the name is one of Simon, John or Phil, tell the user that you think Haskell is a great programming language. If the name is Koen, tell them that you think debugging Haskell is fun (Koen Classen is one of the people who works on Haskell debugging); otherwise, tell the user that you don't know who he or she is.(As far as syntax goes there are a few different ways to do it; write at least a version using
Actions under the microscope
Actions may look easy up to now, but they are a common stumbling block for new Haskellers. If you have run into trouble working with actions, see if any of your problems or questions match any of the cases below. We suggest skimming this section now, then come back here when you actually experience trouble.
Mind your action types
One temptation might be to simplify our program for getting a name and printing it back out. Here is one unsuccessful attempt:
Example: Why doesn't this work?
main = do putStrLn "What is your name? " putStrLn ("Hello " ++ getLine)
HaskellWikiBook.hs:3:26: Couldn't match expected type `[Char]' against inferred type `IO String'
Let us boil the example above down to its simplest form. Would you expect this program to compile?
Example: This still does not work
main = do putStrLn getLine
For the most part, this is the same (attempted) program, except that we've stripped off the superfluous "What is your name" prompt as well as the polite "Hello". One trick to understanding this is to reason about it in terms of types. Let us compare:
putStrLn :: String -> IO () getLine :: IO String
We can use the same mental machinery we learned in Type basics to figure how this went wrong.
putStrLn is expecting a
String as input. We do not have a
String; we have something tantalisingly close: an
IO String. This represents an action that will give us a
String when it's run. To obtain the
putStrLn wants, we need to run the action, and we do that with the ever-handy left arrow,
Example: This time it works
main = do name <- getLine putStrLn name
Working our way back up to the fancy example:
main = do putStrLn "What is your name? " name <- getLine putStrLn ("Hello " ++ name)
Now the name is the String we are looking for and everything is rolling again.
Mind your expression types too
So, we've made a big deal out of the idea that you can't use actions in situations that don't call for them. The converse of this is that you can't use non-actions in situations that expect actions. Say we want to greet the user, but this time we're so excited to meet them, we just have to SHOUT their name out:
Example: Exciting but incorrect. Why?
import Data.Char (toUpper) main = do name <- getLine loudName <- makeLoud name putStrLn ("Hello " ++ loudName ++ "!") putStrLn ("Oh boy! Am I excited to meet you, " ++ loudName) -- Don't worry too much about this function; it just converts a String to uppercase makeLoud :: String -> String makeLoud s = map toUpper s
This goes wrong...
Couldn't match expected type `IO' against inferred type `' Expected type: IO t Inferred type: String In a 'do' expression: loudName <- makeLoud name
This is similar to the problem we ran into above: we've got a mismatch between something expecting an IO type and something which does not produce IO. This time, the trouble is the left arrow
<-; we're trying to left-arrow a value of
makeLoud name, which really isn't left arrow material. It's basically the same mismatch we saw in the previous section, except now we're trying to use regular old String (the loud name) as an IO String. The latter is an action, something to be run, whereas the former is just an expression minding its own business. We cannot simply use
loudName = makeLoud name because a
do sequences actions, and
loudName = makeLoud name is not an action.
So how do we extricate ourselves from this mess? We have a number of options:
- We could find a way to turn
makeLoudinto an action, to make it return
IO String. However, we don't want to make actions go out into the world for no reason. Within our program, we can reliably verify how everything is working. When actions engage the outside world, our results are much less predictable. An IO
makeLoudwould be misguided. Consider another issue too: what if we wanted to use makeLoud from some other, non-IO, function? We really don't want to engage IO actions except when absolutely necessary.
- We could use a special code called
returnto promote the loud name into an action, writing something like
loudName <- return (makeLoud name). This is slightly better. We at least leave the
makeLoudfunction itself nice and IO-free whilst using it in an IO-compatible fashion. That's still moderately clunky because, by virtue of left arrow, we're implying that there's action to be had -- how exciting! -- only to let our reader down with a somewhat anticlimactic
return(note: we will learn more about appropriate uses for
returnin later chapters).
- Or we could use a let binding...
It turns out that Haskell has a special extra-convenient syntax for let bindings in actions. It looks a little like this:
let bindings in
main = do name <- getLine let loudName = makeLoud name putStrLn ("Hello " ++ loudName ++ "!") putStrLn ("Oh boy! Am I excited to meet you, " ++ loudName)
If you're paying attention, you might notice that the let binding above is missing an
in. This is because
let bindings inside
do blocks do not require the
in keyword. You could very well use it, but then you'd have messy extra do blocks. For what it's worth, the following two blocks of code are equivalent.
do name <- getLine let loudName = makeLoud name putStrLn ("Hello " ++ loudName ++ "!") putStrLn ( "Oh boy! Am I excited to meet you, " ++ loudName)
do name <- getLine let loudName = makeLoud name in do putStrLn ("Hello " ++ loudName ++ "!") putStrLn ( "Oh boy! Am I excited to meet you, " ++ loudName)
At this point, you have the fundamentals needed to do some fancier input/output. Here are some IO-related topics you may want to check in parallel with the main track of this course.