🎓 A1Learner
🔍
0 of 54 lessons completed

Haskell IO Actions

An IO action is a step that talks to the outside world. The main program is one IO action.

putStrLn is an action

putStrLn builds an action that prints text. Here that action is main, so running the program prints the line.

main :: IO ()
main = putStrLn "Hello, Ana"

print is an action

print is also an IO action. It prints the value that its argument works out to.

main :: IO ()
main = print (19 + 23)

A pure value shown by an action

The binding total is a pure value. The print action shows it.

total = 20 + 22
main :: IO ()
main = print total

Example

greeting = "Finished"
score = 90 + 5
main :: IO ()
main = putStrLn (greeting ++ ": " ++ show score)

Key points

  • IO action: a step that can print, read, or change something outside the program.
  • main: the IO action Haskell runs first, and its type is IO ().
  • putStrLn: an IO action that prints text and moves to a new line.
  • print: an IO action that prints a value in its Show form.
  • Pure value: a value worked out inside the program, with no outside step.
  • Running an action: an action does its outside step only when Haskell runs it.

In this example, greeting and score are pure values worked out inside the program. The main action joins them into one line of text. Running main prints that line.

Note

a pure value on its own prints nothing, so an action is needed to show it.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Haskell Playground

Exercise

Which one of these is an IO action?