MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

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[1]:= Head[Hold[Simplify[1 + 2 x + x^2]]]

Out[1]= Hold

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

Out[2]= 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[3]:= Hold[Simplify[1 + 2 x + x^2]]

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

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

                    2
Out[4]= 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
borrowed from MathLink terminology)

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

Out[5]= 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[6]:= Trace[(2+3)[Simplify[1 + 2 x + x^2]]]

                                          2          2            2
Out[6]= {{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[7]:= SetAttributes[myFunction,HoldAll]

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

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

Out[9]= 5[t]

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

Out[10]= a[t, t]

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

                     2
Out[11]= Hold[(1 + x) ]

Hope this helps clarifying things, Tobias

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


  • Prev by Date: Race Condition
  • Next by Date: Re: How to interrupt a running evaluation in MathLink
  • Previous by thread: Re: Evaluation of args in pure functions
  • Next by thread: Re: Evaluation of args in pure functions