[Haskell-beginners] where scope

Isaac Dupree ml at isaac.cedarswampstudios.org
Mon Jul 20 02:14:17 EDT 2009


I. J. Kennedy 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.

the reason we need "where", is to scope over a function clause.  Do you 
know about guards?  like, here's a (very stupid) example where "let" 
doesn't work, so "where" scoping is needed:

f n
   | np == 0 = 1 + add
   | np == 1 = 3 + add
  where
    np = n + 1
    add = n + 2

-Isaac



More information about the Beginners mailing list