
19 May
2007
19 May
'07
2:48 p.m.
David House wrote:
On 19/05/07, Andrew Coppin
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 ...