Haskell encourages a useful habit: say what a result looks like first, and work out how to produce it afterwards. That becomes concrete when a function needs to represent the absence of an answer.
Make absence part of the type
Consider division that explicitly handles a zero denominator:
safeDivide :: Double -> Double -> Maybe Double
safeDivide _ 0 = Nothing
safeDivide x y = Just (x / y)
formatResult :: Maybe Double -> String
formatResult Nothing = "Cannot divide by zero"
formatResult (Just x) = show xThe result type distinguishes a value from its absence. A caller can pattern-match on those cases. This improves the interface; it does not prove that every caller handles both cases. Incomplete patterns and partial functions can still fail at runtime.
data defines a new data type. type defines a synonym for an existing type. Those are different tools, even when both make a signature easier to read.
Functions can return functions
The arrows in a function type associate to the right:
add :: Int -> Int -> Int
add x y = x + y
addTen :: Int -> Int
addTen = add 10Read the first signature as Int -> (Int -> Int). Supplying one argument produces a function waiting for the second. Partial application follows naturally from that representation.
Laziness changes when work happens
Haskell has non-strict evaluation semantics. Lazy evaluation is the usual implementation strategy. For example:
firstFiveSquares :: [Integer]
firstFiveSquares = take 5 [n * n | n <- [1..]]
-- [1,4,9,16,25]The program can consume a finite prefix without constructing the entire infinite list. Replacing take 5 with sum asks for something different: a result requiring the whole list, so this example would not finish.
Delaying work can also retain data or build up unevaluated expressions. Laziness is a tradeoff to understand, not a guarantee of low memory use. The Haskell 2010 Report specifies the language’s expression semantics.
Shorter code is not automatically cheaper
For singly linked lists, (++) traverses its left operand. If there are chunks of elements each, left-associated concatenation repeatedly traverses an accumulating prefix:
That accounts for traversal work, assuming the concatenated output is consumed. Right-associated concatenation traverses each chunk as a left operand at most once, giving work under the same assumptions.
The cost comes from the data structure and association, not from a rule that foldr is always fast and foldl is always slow.
Purity does not mean a program cannot do anything
An expression of type IO a describes an action that can produce an a. The runtime executes the program’s main action. Pure code can construct and combine descriptions of effects without treating arbitrary effectful execution as an ordinary pure function call.
Property-based tests can then check claims across generated inputs. They can expose counterexamples, but passing many cases is not a proof for every possible input.
What I find useful here is the pressure to state assumptions clearly: where absence is possible, which data is demanded, and what a type does and does not guarantee.
What the type leaves open
I would ask what a type rules out before treating a type-correct program as evidence that the idea is right. A function returning Maybe a makes absence explicit, but it does not tell us whether returning Nothing was the correct decision. That distinction is where I would start writing properties and looking for counterexamples.