
On Mon, Jul 20, 2009 at 7:29 AM, I. J. Kennedy
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
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
And certainly not as : 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ï