Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #4743 > unrolled thread

Visibility of value of variable bindings in module

Started bycaw <cawright.99@gmail.com>
First post2011-09-04 22:06 +0000
Last post2011-09-05 11:23 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  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

#4743 — Visibility of value of variable bindings in module

Fromcaw <cawright.99@gmail.com>
Date2011-09-04 22:06 +0000
SubjectVisibility 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]


#4762

From"Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com>
Date2011-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