[Haskell-cafe] A small error
Andrew Coppin
andrewcoppin at btinternet.com
Sat May 19 10:48:06 EDT 2007
David House wrote:
> 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.
Ouch!
> 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
Well, in my case the thing in each do block is an actual IO action. (It
asks the user some questions, rather than just generating a constant.)
How about this?
do
y <- if x < 0
then do ...
else do ...
More information about the Haskell-Cafe
mailing list