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


Groups > comp.soft-sys.math.mathematica > #8347

Re: Funny Behavior of Module

From "Nasser M. Abbasi" <nma@12000.org>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Funny Behavior of Module
Date 2012-02-04 11:40 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jgj5em$n5b$1@smc.vnet.net> (permalink)
References <jgg195$bnb$1@smc.vnet.net>

Show all headers | View raw


On 2/3/2012 1:10 AM, Louis Talman wrote:
> I know that Module exhibits some funny behavior, but this behavior is
> something I've never seen before. It seems wrong to me.
>
> In a new kernel session enter
>
> f[x_] := Module[{a},a = x^2; x = y; a]
>
> f[z]
>
> z
>
> Mathematica returns y for z, as it ought to. (In fact, the purpose of
> the code was to demonstrate to students what can happen when a Module
> monkeys with its formal parameters.)
>
> But f[z] returns y^2. It seems to me that it should return z^2, but that
> for some reason it's interpreting the first Set instruction in the body
> of the Module as a SetDelayed instruction. (We get exactly the same
> behavior if we replace that Set with SetDelayed in the code.)
>
> Can anyone explain what's going on here? ITABOAF?
>
>
> --Lou Talman
>    Department of Mathematical&  Computer Sciences
>    Metropolitan State College of Denver
>
>    <http://rowdy.mscd.edu/%7Etalmanl>
>

The program is not really a valid Mathematica code, inside
the function, the pattern x_ should always show up on the RHS
of any assignment, not on the LHS.
  
In your example this line

    x=y

is not valid, since the pattern x shows on the left side of
an assignment not on the right side.

As what happens when you wrote it the way you did:

a=x^2 ===> this becomes a=z^2 since x is z
next, you say x=y, which became z=y, since x is z.
next, the last thing you have is 'a'. Now Mathematica
will evaluate 'a', which is z^2. But now 'z' is 'y',
so it does one more transformation rule, Hence 'a' which is 'z^2'
evaluates to 'y^2'

And this is what you see.

This is no different than writing

------------------
Remove["Global`*"]
a=z^2;
z=y;
a
-------------------

and now you'll see that 'a' is y^2 and not z^2. Since
'a' evaluates 'a' to z^2, and 'z' evaluates to 'y^2',
and now the evaluator stops, since there is no more
transformation rules to apply.

One way to do what you did is by introducing a local symbol

-----------------------
Remove["Global`*"]
f[$x_] := Module[{a, x = $x}, a = x^2; x = y; a]
f[z]
--------------------------

now it returns  z^2. The reason the above returns z^2 and not
y^2, is because:

a=x^2 ===> this becomes a=z^2 since x is z
next, x=y, but now x is the local x and not z. So, x becomes y.
next, we have'a'. Now Mathematica will evaluate 'a', which is z^2,
and no more. z still is z. So what you get is z^2.

--Nasser

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Funny Behavior of Module Louis Talman <talmanl@gmail.com> - 2012-02-03 07:10 +0000
  Re: Funny Behavior of Module "Oleksandr Rasputinov" <oleksandr_rasputinov@ymail.com> - 2012-02-04 11:28 +0000
  Re: Funny Behavior of Module Roger Wilson <rogerhw999@gmail.com> - 2012-02-04 11:39 +0000
  Re: Funny Behavior of Module "Nasser M. Abbasi" <nma@12000.org> - 2012-02-04 11:40 +0000

csiph-web