       Re: Evaluation of args in pure functions

• To: mathgroup at smc.vnet.net
• Subject: [mg16972] Re: Evaluation of args in pure functions
• From: tobiasoed at my-dejanews.com
• Date: Sat, 10 Apr 1999 02:13:21 -0400
• Organization: Deja News - The Leader in Internet Discussion
• Sender: owner-wri-mathgroup at wolfram.com

```Hi Andrea,

First let me use Hold instead of Unevaluated. Although
this doesn't change anything here Hold is more appropriate
than Unevaluated:
Unevaluated is to be used when you want a function to
get an argument without it being evaluated.
With Hold the function gets the argument wrapped with Hold.
With Unevaluated the function gets the argument unevaluated
but not wrapped with Unevaluated.
An example may be clearer

In:= Head[Hold[Simplify[1 + 2 x + x^2]]]

Out= Hold

In:= Head[Unevaluated[Simplify[1 + 2 x + x^2]]]

Out= Simplify

In Your case there is no difference between the way
Unevaluated and Hold behave because you don't feed
the expression into a function, so the Unevaluted
behaves just as a Hold.
As you can see this is your problem reformulated
using Hold,

In:= Hold[Simplify[1 + 2 x + x^2]]

2
Out= Hold[Simplify[1 + 2 x + x ]]

In:= Hold[#]& @ Simplify[1 + 2 x + x^2]

2
Out= Hold[(1 + x) ]

As you can see the behaviour is the same.

The second result can be explained when you write
it in a form closer to FullForm:

Function[Hold[#]] [Simplify[1 + 2 x + x^2]]

This is a Composit expression. (I mean that the Head of
it is not a symbol but a Mathematica expression - Composite is

In:= Head[Unevaluated[Function[Hold[#]] [Simplify[1 + 2 x + x^2]]]]

Out= Hold[#1] &

The question now is how Composite expressions are evaluated.
First the head is evaluated then the arguments are; resulting
in a new expression with the evaluated head and arguments
as in

In:= Trace[(2+3)[Simplify[1 + 2 x + x^2]]]

2          2            2
Out= {{2 + 3, 5}, {Simplify[1 + 2 x + x ], (1 + x) }, 5[(1 + x) ]}

Finally this expression is evaluated by looking for rules
associated with the new Head (now only !).

In the case where the starting Composite expression has
Function[something] as Head, the something is not evaluated
because Function has attribute HoldAll. Thus the evaluation
of such an expression skips the first step. In the final
step the arguments of the function are substituted into the
function due to rules associated with function.

Here is myFunction that simulates Function with a single
argument:

In:= SetAttributes[myFunction,HoldAll]

In:= myFunction[f_] [args_]:=(
ReplacePart[f,args,Position[f,Slot]]
)

In:= myFunction[(2+3)[#]] [t]

Out= 5[t]

In:= myFunction[a[#,#]] [t]

Out= a[t, t]

In:= myFunction[Hold[#]] [Simplify[1 + 2 x + x^2]]

2
Out= Hold[(1 + x) ]

Hope this helps clarifying things, Tobias

-----------== Posted via Deja News, The Discussion Network ==----------