A pure function consumes input and produces output without side effects. A side effect is an interaction with program state outside the function, such as working with the operating system, mutable state, or shared multithreaded resources. Keeping a function pure makes its behavior observable through its output alone. section 1.5 section 3.2
In a practical program, separate transformation work from environmental work. Reading command-line arguments, reading files, and printing require IO actions. Parsing arguments and numbering, filtering, or formatting file lines can remain pure because they only transform strings or select between cases. section 3.2 section 3.2.2
Favor pure code where possible because it:
- Can be tested directly against a specification without adapter classes, fake environments, or production-like setup. section 1.2.3
- Supports unit and property testing without constructing a fitting test environment. section 3.2.1
- Keeps side effects contained in actions rather than exposing them throughout the program. section 3.2.1
Uppercasing input while isolating IO
An IO action can read a line, pass it to the pure transformation map toUpper, then print the result. The action handles getLine and putStrLn; map and toUpper perform the side-effect-free transformation. section 3.2.1
Listing 3.3 A program that reads a line from the user and prints its uppercase representation
module Main (main) where import Data.Char #1 main :: IO () main = do line <- getLine #2 putStrLn (map toUpper line) #3
This listing makes the boundary visible: input and output stay in main, while map toUpper transforms the received string. section 3.2.1
This separation is a design choice for reasoning and testing, not a claim that compiled components remain physically separate. While writing code, track where side effects can affect program semantics and reduce the impure portion when possible. section 3.2.2


















