# Let vs. Where

### From HaskellWiki

Haskell programmers often wonder, whether to use or .
is an expression,
is bound to a surrounding syntactic construct,
and later you decide to put this into the monad.
will not work, because refers to the pattern matching ,
where no is in scope.
In contrast, if you had started with , then you wouldn't have trouble.

let

where

This seems to be only a matter of taste in the sense of "Declaration vs. expression_style", however there is more about it.

It is important to know thatlet ... in ...

that is, it can be written whereever expressions are allowed.

In contrast to that,where

like the pattern matching line of a function definition.

Consider you have the function

f :: s -> (a,s) f x = y where y = ... x ...

Control.Monad.State

However, transforming to

f :: State s a f = State $ \x -> y where y = ... x ...

where

f =

x

let

f :: s -> (a,s) f x = let y = ... x ... in y

This is easily transformed to:

f :: State s a f = State $ \x -> let y = ... x ... in y