[Haskell-beginners] where scope

Chaddaï Fouché chaddai.fouche at gmail.com
Mon Jul 20 06:54:13 EDT 2009


On Mon, Jul 20, 2009 at 7:29 AM, I. J. Kennedy<jack at realmode.com> wrote:
> GHC complains that  u  is out of scope in the following definition:
> f n =
>   let u = [1..n] in g n
>   where
>     g x = u
> Why?  I understand that it's "just the way Haskell works", but I'm
> wondering about the rationale of the language designers when deciding
> on this scoping behavior.

"let ... in" and "where" have fundamentally different scoping
behaviour : "where" is attached to a function definition clause (a
pattern matching, eventually several guards), "let ... in ..." is an
expression like any other.
"expression where ..." isn't valid Haskell, only "function clause where ..." is.

You should see your code as :
> (f n = let u = [1..n] in g n)
>   where g x = u

Not as :
> f n = (let u = [1..n] in g n) where g x = u

And certainly not as :
> f n = let u = [1..n] in (g n where g x = u)
which was probably the view you had initially that brought you to
think that "u" would be in scope.

In other words, "let" and "where" are not the interchangeable syntax
for which beginners often take them, they're not just a matter of
taste, each has its own usages that the other can't properly emulate.

-- 
Jedaï


More information about the Beginners mailing list