Free preview.You're sampling one lesson — enroll free to unlock all 8 lessons and track your progress.
Enroll free
lesson

Pure Functions

Pure Functions

In this lesson — part of Functional Foundations — you'll learn pure functions in Haskell and why it matters in real work.

Why it matters

Functional style — pure functions and immutability — makes code easier to test and reason about.

Key ideas

  • Pure functions
  • Immutability
  • No shared mutable state
  • Predictable outputs

In practice

Here's how it looks in idiomatic Haskell:

-- Pure recursion with pattern matching; no mutation, no side effects
factorial :: Integer -> Integer
factorial 0 = 1
factorial n = n * factorial (n - 1)

sumList :: [Int] -> Int
sumList = foldr (+) 0          -- a fold replaces an imperative loop

Haskell note: Purity plus laziness means functions are referentially transparent — you can replace any call with its result, which is what makes equational reasoning and aggressive compiler optimization sound.

Try it yourself

Exercise: In Haskell, rewrite a function that mutates a global so it's pure.

Recap

You now understand pure functions and can apply it in Haskell. Mark this lesson complete and continue to the next one.