do let in
Bernard James POPE
bjpop@cs.mu.OZ.AU
Tue, 4 Mar 2003 15:29:48 +1100 (EST)
Hi,
> For the reason that I'm lazy and don't want to have to modify all my functions
> which use afact, or call functions which use afact, and don't see why I should
> have to -- they were able to call the 'fact' function as a global, and can
> refer to a global 'afact' if I define it outside of main with a fixed value.
> I don't see why having a global dependent on outside input should be so much
> harder.
Of course afact depends on the value of n, which is only known in main.
So you need a way of passing n to afact, and you get the same problem as
before.
> > So perhaps you want to simulate a global variable.
>
> Yeah. And I've tried various permutations
>
> let afact = ... in
> let (glo, ...
>
> let afact = ... in
> (glo, ...
>
> let afact = ... in
> (glo, ...
>
> let afact = ...
> let (glo...
None of these will work because of the scoping rules in Haskell (which are
static, perhaps you are used to languages with dynamic scope?).
If you follow the desugaring rules for do notation, it might be clearer how
the scoping rules work. I posted a reference in my previous mail.
So if you want a global variable - read the paper by Hughes that I mentioned
previously. It is short, easy to understand, and covers the typical
ways Haskell programmers might try to do it (dirty and clean). It might even
clarify the scoping issues involved.
If you can't be bothered to read the paper, then I'm afraid you'll have to
thread the value of afact through your code.
Cheers,
Bernie.