Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #4743 > unrolled thread
| Started by | caw <cawright.99@gmail.com> |
|---|---|
| First post | 2011-09-04 22:06 +0000 |
| Last post | 2011-09-05 11:23 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Visibility of value of variable bindings in module caw <cawright.99@gmail.com> - 2011-09-04 22:06 +0000
Re: Visibility of value of variable bindings in module "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-09-05 11:23 +0000
| From | caw <cawright.99@gmail.com> |
|---|---|
| Date | 2011-09-04 22:06 +0000 |
| Subject | Visibility of value of variable bindings in module |
| Message-ID | <j40spg$l13$1@smc.vnet.net> |
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
[toc] | [next] | [standalone]
| From | "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> |
|---|---|
| Date | 2011-09-05 11:23 +0000 |
| Message-ID | <j42bf2$ru4$1@smc.vnet.net> |
| In reply to | #4743 |
On Sun, 04 Sep 2011 23:06:40 +0100, caw <cawright.99@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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web