Day 01 Foundations

Pure Functions & Types

Haskell is a purely functional language with a strong static type system. Functions have no side effects, data is immutable by default, and the type checke

~1 hour Hands-on Precision AI Academy

Today's Objective

Functions are defined with equations.

01

Purity and Immutability

A pure function always returns the same output for the same input and has no side effects. In Haskell, this is enforced by the type system — a function that does I/O must have the IO type in its signature, making side effects explicit and visible. Immutability means variables never change; instead, you create new values. This makes reasoning about code far easier and enables safe parallelism.

02

The Haskell Type System

Haskell's type system is Hindley-Milner — it infers types without annotations in most cases. Types: Int, Integer (arbitrary precision), Double, Bool, Char, String ([Char]), Maybe a (optional value), Either e a (error or value), and [a] (list). Type classes define behavior: Eq (equality), Ord (ordering), Show (to string), Num (arithmetic), Functor, Monad. Types are checked at compile time; runtime type errors are essentially impossible.

03

Function Definition and Pattern Matching

Functions are defined with equations. Pattern matching deconstructs values: match on constructors, literals, or wildcards (_). Guards add conditions. Where clauses define local bindings. Let expressions define local bindings inside an expression. Function application is left-associative and highest precedence: 'f x y' means '(f x) y'. The ($) operator applies a function to an argument: 'f $ g x' means 'f (g x)'.

haskell
haskell
-- Type signatures are optional but idiomatic
factorial :: Integer -> Integer
factorial 0 = 1
factorial n = n * factorial (n - 1)

-- Pattern matching on lists
listSum :: [Int] -> Int
listSum []     = 0
listSum (x:xs) = x + listSum xs

-- Guards
bmiCategory :: Double -> String
bmiCategory bmi
  | bmi < 18.5 = "Underweight"
  | bmi < 25.0 = "Normal"
  | bmi < 30.0 = "Overweight"
  | otherwise  = "Obese"

-- Where clause
circleArea :: Double -> Double
circleArea r = pi * r2
  where r2 = r * r

-- Let expression
cylinderVolume :: Double -> Double -> Double
cylinderVolume r h =
  let area = pi * r * r
  in  area * h
💡
Write type signatures for all top-level functions even though Haskell can infer them. They serve as documentation, and the compiler will tell you if your implementation doesn't match your stated intent.

Supporting References & Reading

Go deeper with these external resources.

Docs
Pure Functions & Types Official documentation for haskell.
GitHub
Pure Functions & Types Open source examples and projects for Pure Functions & Types
MDN
MDN Web Docs Comprehensive web technology reference

Day 1 Checkpoint

Before moving on, confirm understanding of these key concepts:

Continue To Day 2
Day 2 of the Haskell in 5 Days course