Re: Visibility of value of variable bindings in module
- To: mathgroup at smc.vnet.net
- Subject: [mg121234] Re: Visibility of value of variable bindings in module
- From: "Oleksandr Rasputinov" <oleksandr_rasputinov at hmamail.com>
- Date: Mon, 5 Sep 2011 07:07:25 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <j40spg$l13$1@smc.vnet.net>
On Sun, 04 Sep 2011 23:06:40 +0100, caw <cawright.99 at gmail.com> wrote:
> Hi
>
> Module[{x = Sum[i, {i, Length[{1, 2, 3}]}], y = x + 1}, Print[y];
> Print[x]]
>
> prints out:
>
> 1 + x
> 6
>
> So, in the variable declaration section of the Module statement, the
> binding of x to 6 isn't visible to y.
> (It's a bit like let and let*, but not)
>
> Is there a form which will allow the bindings to be visible within the
> variable declaration section?
>
> thanks very much
>
> chris
>
No, there is not. Instead you must do:
Module[{x, y},
x = Sum[i, {i, Length[{1, 2, 3}]}];
y = x + 1;
Print[y];
Print[x]
]
or something logically equivalent. If you require these semantics
frequently, you could easily write a function that takes a Module as an
(unevaluated) argument and rewrites it into the required form before
evaluating it.