Haskell where vs let and Which One to Reach For
A student sent me a function in the third week of last term that worked out a shipping band from a weight in grams and a destination code.

A student sent me a function in the third week of last term that worked out a shipping band from a weight in grams and a destination code. It had four guards, and three of them recomputed the same conversion from grams to kilograms, because he had written the division inside a let in the first guard and then pasted it downward. Two copies divided by 1000 and one divided by 1024. The function type checked, his two tests passed, and the wrong band sat there waiting for a parcel heavier than five kilograms.
That is the real question under where against let, and it has nothing to do with taste. A where clause belongs to an entire function equation, guards included. A let is an expression that scopes over its own in branch and stops there. Decide what you need in scope and the choice usually makes itself.
What each one is attached to
A let is an expression. Anywhere you could write x + 1, you can write let y = 2 in x + y. It can sit inside a lambda, inside a list, inside a tuple, inside another let.
A where is not an expression. It is a tail attached to a declaration: one equation of a function, a pattern binding, a class method, an instance method, a module. You cannot drop it into the middle of an expression because there is no declaration there for it to belong to.
bandLet :: Int -> String
bandLet grams =
let kg = fromIntegral grams / 1000 :: Double
in if kg < 2 then "small" else "large"
bandWhere :: Int -> String
bandWhere grams
| kg < 2 = "small"
| kg < 5 = "medium"
| otherwise = "large"
where
kg = fromIntegral grams / 1000 :: Double
The second version is the one the student needed. There is no legal place to put a let that covers all three guards at once, because the guards are part of the equation rather than part of an expression inside it.
Guards are where the difference actually bites
This is the single case that settles most arguments. If a value is used by more than one guard, it has to live in a where, or you copy it and eventually the copies drift. I have graded this exact bug in maybe fifteen submissions over four terms, and it is never the division that students get wrong. It is the fourth copy of it.
The same thing happens with the guards of a case alternative, and it is easy to forget that an alternative may carry its own where:
classify :: (Int, Int) -> String
classify p = case p of
(w, h)
| ratio > 1.6 = "wide"
| ratio < 0.62 = "tall"
| otherwise = "square enough"
where ratio = fromIntegral w / fromIntegral h :: Double
Pattern matching and guards interact with scope in ways worth reading about on their own, and the rules are set out in Haskell pattern matching and guards explained clearly.
The cases that come up every week
| Situation | Reach for | Reason |
|---|---|---|
| Value used by two or more guards | where | Only a where clause covers every guard |
| Helper used by several equations of one function | Top level or where on each | A where belongs to one equation only |
| Inside a do block | let | A statement is not a declaration |
| Inside a lambda or a list comprehension | let | No declaration to attach a where to |
| One intermediate value used once, three lines down | let | Keeps the name next to its use |
| Four named steps that explain the algorithm | where | Headline first, detail underneath |
| Binding you want to export or test | Neither | Lift it to the top level |
The advice to always prefer where does not survive a do block
The received wisdom in teaching material is that where reads better, so default to it. I taught that for two terms and stopped, because the moment students wrote anything with IO in it the rule produced confusion rather than clarity.
In a do block you write let with no in at all, and that form is a statement, not an expression:
report :: FilePath -> IO ()
report path = do
raw <- readFile path
let rows = lines raw
total = length rows
putStrLn (path ++ ": " ++ show total ++ " rows")
You cannot replace that with a where, because rows depends on raw, and raw does not exist until the action has run. A where binding is in scope for the whole equation, which sounds generous until you notice it also means it cannot see anything bound by a statement partway through. That one asymmetry answers most of the questions I get about this.
There is a second reason I stopped giving the blanket advice. A where clause makes the reader jump down the page, which pays off when the names are a genuine vocabulary for the algorithm and costs when there is one trivial value. Three or four named steps under a short, readable equation is good writing. One binding called tmp fifteen lines below its only use is not.
Sharing, and the restriction nobody warns you about
Both forms bind a thunk that is forced at most once for each entry into the enclosing scope. Neither is a macro, so neither recomputes per use. If you call bandWhere a thousand times you get a thousand divisions, with either keyword. Hoisting work out of a loop is a different job, usually done by passing the value in as an argument.
The restriction that bites is the monomorphism restriction, and it applies to both. A binding with no arguments and no type signature gets one concrete type, chosen once:
summary :: [Int] -> (Double, Int)
summary xs = (mean, scaled)
where
n = fromIntegral (length xs)
mean = sum (map fromIntegral xs) / n
scaled = round (n * 2)
That fails to compile under GHC 9.6 with defaults on. GHC must pick a single type for n, and mean wants Double while scaled wants something Integral. The error names an ambiguous type variable and mentions no restriction at all, which is why it eats twenty minutes the first time. The fix is a signature on the binding, not a language extension: write n :: Double in the where clause and convert separately for the rounded value. Turning on NoMonomorphismRestriction makes the error go away and makes the next one harder to read.
Three scope rules that cost real time
A where clause belongs to one equation, not to the function. If you define go [] and go (x:xs) as two equations, a where under the second is invisible to the first. Students read the two equations as one function and expect shared scope. Lift the helper to the top level or repeat it.
A where binding silently shadows an argument. Name a where binding xs when the argument is already xs and it compiles, then behaves strangely. Build with -Wname-shadowing, which is part of -Wall, and treat the warning as an error in your project file.
A where binding can mention arguments; a top level one cannot. This is what keeps helpers in a where clause even when they look generic. If the helper closes over three arguments, moving it to the top level means adding three parameters, and sometimes that is the right trade and sometimes it makes every call site noisy.
Record field names deserve a mention too, since they add names at the top level that can clash with your where bindings. The patterns that avoid that collision are in Haskell record syntax without the field name headaches, and more of this material sits in the programming section.
FAQ
Is one of them faster than the other?
No. GHC desugars a where clause into the same kind of local binding group a let produces, and the generated Core is usually identical. I have compared the output for small functions with -ddump-simpl and never found a difference worth reporting. Choose on scope and readability.
Can I use where inside a do block?
Only at the end of the equation that contains the block, not between statements. Inside the block itself use let without in. A where at the bottom of a function whose body is a do block is perfectly normal and a good home for pure helpers.
Why does my where binding not see the other equation of the same function?
Because the clause attaches to one equation. That is the rule, and it is the most common scope surprise in the first month. If two equations need the same helper, move it out.
Should I put type signatures on local bindings?
On anything numeric or polymorphic, yes, and the monomorphism restriction is the reason. For a one line binding of an obvious type it is noise. I tell students to add the signature the moment the error message mentions an ambiguous type variable, and to add it preemptively for anything involving fromIntegral.
More in Programming
Programming
Haskell Semigroup and Monoid: When Each One Fits
A student brought me a function last term that merged three configuration records into one.
Programming
Haskell Typed Holes for Writing Code With GHC's Help
You know roughly what a function should do, you have written the signature, and then you sit there staring at the right-hand...
Programming
Haskell Functor and Applicative Explained With Real Code
You have a value stuck inside a Maybe, a list, or an IO action, and you keep writing case expressions just to apply one small...