where, let and scope

where under a definition

A where block belongs to a definition, sits under it and indented, and names values that the right-hand side and the guards can use. It is not an expression: it cannot be printed or passed anywhere, it only extends the definition above it.

module Main (main) where basePrice :: Int -> Int basePrice units = units * price where price = 3 withTax :: Int -> Int withTax amount = amount + tax where tax = amount `div` 10 main :: IO () main = do print (basePrice 4) print (withTax 100) print (let square x = x * x in square 6 :: Int)

ghc -Wall -O1 -o scopes scopes.hs
./scopes

12
110
36

The two where blocks are different in an important way: price is a constant that does not depend on the argument, while tax uses amount. Both are legal, because the names in a where block see the arguments of the definition they belong to. A helper that is only used by one function belongs here rather than at the top level: it keeps the module namespace clean and tells the reader where to look.

let ... in

let is an expression: let name = value in expression has the type of the expression and the name is visible only there. The third line of the program is the smallest form, let square x = x * x in square 6, which binds a function rather than a value.

let x = 2 in x * x -- 4 let square x = x * x in square 6 -- 36 let a = 1; b = 2 in a + b -- 3 (let a = 1 in a + 1) * 10 -- 20

A let can bind several names at once, and they can refer to each other, because the bindings are recursive unless a pattern says otherwise. The last line shows that the construct is an expression like any other: it can sit inside a larger one, and parentheses decide how far it reaches. Where a where would need a named function to attach to, a let works in the middle of an expression.

let in do

Inside a do block, let needs no in: the names live for the rest of the block. This is the form most Haskell programs use, because main is a do block and the values it computes are usually named once at the top.

module Main (main) where main :: IO () main = do let name = "Haskell" major = 9 :: Int putStrLn (name ++ " " ++ show major) let doubled = map (* 2) [1, 2, 3] :: [Int] print doubled print (sum doubled)

ghc -Wall -O1 -o dolet dolet.hs
./dolet

Haskell 9
[2,4,6]
12

The indentation is the block structure: major is aligned under name, so the two belong to one let, and putStrLn is back at the indentation of let, so the binding covers it and everything after it. The second let introduces doubled, which the two following statements use. A value that is used once can be written inline; a let earns its place when the name explains what the expression means.

What each one sees

The three forms differ in where the name lives:

A name bound by any of them shadows an outer binding of the same name for the region it covers, and -Wall reports that shadowing as -Wname-shadowing. Shadowing is sometimes deliberate, in a small helper that reuses a short name, but a shadowed argument is usually a mistake: the outer value silently stops being used, and the warning is the only sign.

The practical rule the course follows: where for a helper of one definition, let ... in for a value used inside one expression, and let in a do block for the values of a main or of an action. A top-level definition is for anything that two functions share.

Exercises

  1. Move price out of its where block to the top level and say what changes for a reader of the module.
  2. Write area w h with a where helper for the perimeter and print both values.
  3. Rewrite let square x = x * x in square 6 as a where block, then as a top-level function.
  4. Add a second let in the do block that uses doubled, and explain why it can.
  5. Shadow an argument with a where binding on purpose and read the -Wname-shadowing warning.
  6. Use let to name the result of words "a b c" and print its length.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Where does a where block attach?

    Answer

    To the definition above it, indented under it

    where is part of a definition: it extends the right-hand side and the guards of that one definition.

  2. What does let x = 2 in x * x evaluate to?

    Answer

    4

    The binding is local to the in expression, which is 2 * 2.

  3. What does this program print? module Main (main) where basePrice :: Int -> Int basePrice units = units * price where price = 3 main :: IO () main = print (basePrice 4)

    Answer

    12

    price is 3 and units is 4, so the product is 12.

  4. In a do block, what does let name = value need to be followed by?

    Answer

    Nothing: the name lives for the rest of the block

    In a do block the in is dropped, and the binding covers the remaining statements of the block.

  5. Does a where binding see the arguments of its definition?

    Answer

    Yes, and that is how a helper can use them

    The scope of where includes the arguments, so tax = amount div 10 can refer to amount.

  6. What does this program print? module Main (main) where main :: IO () main = do let name = "Haskell" major = 9 :: Int putStrLn (name ++ " " ++ show major)

    Answer

    Haskell 9

    The string is joined with a space and the number is shown as text, so the line is Haskell 9.

  7. Which construct is an expression rather than a definition?

    Answer

    let ... in ...

    let ... in produces a value and has a type; where only extends the definition above it.

  8. What does -Wname-shadowing report?

    Answer

    A local binding hides an outer binding of the same name

    Shadowing is a new binding of a name that already exists in an enclosing scope, which -Wall warns about.

  9. How far does a let binding inside a do block reach?

    Answer

    To the end of the block, including later statements

    The binding covers the rest of the do block, which is why a later statement can use it.

  10. What does this program print? module Main (main) where main :: IO () main = print (let doubled = map (* 2) [1, 2, 3] :: [Int] in sum doubled)

    Answer

    12

    The local list is doubled to [2,4,6] and then summed, which is 12.

The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev