       Re: Surprising behaviour with nested pure functions and

• To: mathgroup at smc.vnet.net
• Subject: [mg109024] Re: Surprising behaviour with nested pure functions and
• From: "Christoph Lhotka" <christoph.lhotka at univie.ac.at>
• Date: Sat, 10 Apr 2010 06:56:09 -0400 (EDT)

```Hello,

the experiment is very interesting; my point of view:

> In:== Function[{x}, Function[{y}, x + y]]
> Out== Function[{x}, Function[{y}, x + y]]
>
> In:== %[2y]
> Out== Function[{y\$}, 2 y + y\$]

therefore the (Mathematica evaluation process) rule (lets call it RULE1)
is, that local variables are renamed before substitution of the global
variable leading to the above result.

> In:==  func == Function[{x}, Function[{y}, Function[{z}, x + y + z]]]
> Out==  Function[{x}, Function[{y}, Function[{z}, x + y + z]]]
>
> In:==  func[a][b][c]
> Out==  a + b + c

it is the same example as above (RULE1) but with no naming conflicts.

> So far, everything is fine and dandy. Let's try to make it "fail".
>
> In:==  func[a]
> Out==  Function[{y\$}, Function[{z\$}, a + y\$ + z\$]]

it is again an evaluation following the RULE1. The variable x, y, z were
replaced (localized) to x\$, y\$, z\$. x\$ was replaced by a value called a.
again nothing really new, it is RULE1.

> In:==  %[z\$]
> Out==  Function[{z\$}, a + z\$ + z\$]

another Mathematica rule (lets call it RULE2) seems to say: if a variable
is already localized, there is no need to localize it again.

It is important to realize, that we are allowed to access/use localized
variables (I call it now FEATURE1 not RULE3) from outside the function.
This is of course critical, but on the other hand, would you like to miss
this feature, if we were not allowed to?

To this end it is not suprising for me, that y (which was localized to y\$)
is now replaced by z\$ (I guess that the body of the function is hold
unevaluated, which is the only reason, why it is not immediatly simplified
to a+2*z\$).

Therefore the specification of the last argument z (localized to z\$)
replaced by a will result in

> In:==  %[a]
> Out==  3 a

which is what I would like to have (to be consistent wirh RULE1, RULE2 and
FEATURE1).

I agree that one could modify RULE2 into something like

"A localized variable has to be localized anytime the corresponding
function is called" (Lets call it ALTERNATIVE1).

or

"Localized variables can not be accessed from outside the function"
(Remove FEATURE1)

In that case we would (maybe) get:

> In:==  func[a]
> Out==  Function[{y\$}, Function[{z\$}, a + y\$ + z\$]]
> In:==  %[z\$]
> Out==  Function[{z\$}, a + z\$ + z\$\$]
> In:==  %[a]
> Out==  2 a + \$z

but it is not implemented in that way.

> In:== func[y\$][z\$][a]
> Out==  3 a

reveals the evaluation order of the expression:

According to rule RULE1 x,y,z are localized to x\$,y\$,z\$, then \$x is
replaced by the local variable y\$, the local variable y\$ is replaced by
z\$, (according to FEATURE1 also the local variable y\$ which was replaced
just at the previous step !), then \$z is replaced by an a leading to 3a,
in short notation:

(x,y,z)->(x\$,y\$,z\$)->(y\$,y\$,z\$)->(z\$,z\$,z\$)->(a,a,a)->3a

So it is consistent. don=C2=B4t you think so?

This kind of examples help to understand how Mathematica works. I would
like to have more of them. Thank you.

Christoph

```

• Prev by Date: Re: Estimating memory usage of expressions
• Next by Date: Re: if using Mathematica to solve an algebraic problem
• Previous by thread: Re: How to enter an integral in a formula and have mathematica
• Next by thread: Root again