liveBook · Manning

Pure function

As explained in Learn Haskell by Example

A pure function is a function whose result depends only on its inputs and that produces no observable side effects.

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

How Learn Haskell by Example covers it

How deep it goes
Explains the pure-versus-IO boundary through program tasks and testing rationale, without detailing compilation behavior.
Examples
Uses an uppercase line reader and a file line-numbering tool with parsing, filtering, formatting, and printing tasks.
What it assumes
Assumes familiarity with map while demonstrating its use with Data.Char.toUpper inside an IO action.
What you can do after
Apply it via IO actions by keeping input, file access, and output impure while transforming data with pure functions.
How it connects
Connects pure functions with side effects, IO actions, unit testing, property testing, map, and toUpper.

Also explained in

Compare how 19 books explain pure function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.