Lesson 43 of 55
10 mins readPython The IO Monad & Pure/Impure Separation
In Plain English
Haskell maintains mathematical purity by quarantining side effects (terminal output, network calls, filesystem operations) inside the IO monad. Pure functions cannot perform IO.
Deep Dive: How It Works
IO Monad: An IO a value is a description of an action that, when executed by the Haskell runtime, interacts with the real world and produces a value of type a.
Extraction in do: Inside IO do blocks, value <- action runs the action and binds its result to value.
print vs putStrLn: putStrLn takes a String; print x is shorthand for putStrLn (show x).
Core Rules to Remember

Guaranteed Purity Boundary: You cannot call IO from a pure function, preventing accidental side effects.

print = putStrLn . show: Print any Show-compatible type directly to the terminal.
Live Interactive Example
Hit Run Code to see it liveIO Actions and Pure Helper Separation
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
Output Console
Click "Run Code" to view the rendered output.
How it works: formatReport is a pure function; main orchestrates execution and terminal output.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Print Formatted Text with IO
Print `"Haskell IO action executed successfully"`.
1
2
3
4
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.