[Haskell-cafe] A small error

David House dmhouse at gmail.com
Sat May 19 10:46:00 EDT 2007


On 19/05/07, Andrew Coppin <andrewcoppin at btinternet.com> wrote:
> Um... why doesn't this work?
>
> my_fn x = do
>   if x < 0
>     then do
>       let y = 5
>       return ()
>     else do
>       let y = 8
>       return ()
>
>   print y
>
> (Error: "y" is not in scope.)

let-blocks only scope over the do-block they're contained in. Here y
is out of scope once you leave the inner do-block. The solution is to
do something like the following:

do
  let y = if x < 0 then 5 else 8
  print y

-- 
-David House, dmhouse at gmail.com


More information about the Haskell-Cafe mailing list