Application Center - Maplesoft

# Maple Programming: 2.6: Last name evaluation

You can switch back to the summary page by clicking here.

2.06.mws

Programming in Maple

Roger Kraft
Department of Mathematics, Computer Science, and Statistics
Purdue University Calumet

roger@calumet.purdue.edu

2.6. Last name evaluation

Maple uses the full evaluation rule most of the time, but as we have already seen, there are exceptions to this rule. Three exceptions that we have seen so far are delayed evaluation (i.e. using right quotes), forcing certain levels of evaluation with the eval  command, and not evaluating names on the left hand side of an assignment operator. Another exception to the full evaluation rule is the last name evaluation rule  that is used for the names of tables, arrays, procedures, and Maple functions. We have not discussed tables, arrays, and procedures yet, so for now we will only use Maple functions as examples for the last name evaluation rule. Here are some examples that demonstrate this rule. Let us define a Maple function named f .

 > f := x -> x^2-1;

If we ask Maple to evaluate the name f , it just returns f .

 > f;

To find out the definition of f  we have to use the eval  command to force full evaluation of the name f .

 > eval( f );

Now let h  be a name for g  and let g  be a name for f .

 > h := 'g';

 > g := 'f';

Now ask Maple to evaluate the names g  and h .

 > g;

 > h;

Notice that the evaluation of both g  and h  stopped with the name f . On the other hand, if we force full evaluation of either g  or h  then we get the definition of f .

 > eval( g );

 > eval( h );

Now we can state the last name evaluation rule. If the full evaluation of a name would return the definition of a Maple function (i.e., an expression that uses the arrow operator), then Maple will stop the evaluation at the last name it reaches just before the function definition and Maple will return that name. Hence the term "last name evaluation" for the evaluation rule of names that point to a function definition.

 >

But Maple does not always use last name evaluation for names that evaluate fully to a function definition. Here is a counter example.

 > f:='f': g:='g': h:='h': w:='w':

 > f := g;

 > g := h;

 > h := k;

 > k := x -> x^2;

If we now ask Maple to evaluate just the name f , then it will use last name evaluation.

 > f;

But if we ask Maple to evaluate the functional notation f(w) , then Maple will not use last name evaluation.

 > f(w);

If Maple had used last name evaluation in this last command, then the result would have been k(w) . Maple instead used full evaluation on the name f  which returned the definition of k  which Maple then applied to the input of the function, w , and returned .

 >

Maple uses last name evaluation when it evaluates a name of a function in isolation. But if the function name is part of a functional notation, then Maple uses full evaluation of the function name so that it can get to the definition of the function (if there is one). We will say more about this in the next section.

 >

We end this section with an example where the eval  command uses last name evaluation when it is supposed to be using full evaluation. Make the following two assignments.

 > f := 'g'+4; g := x -> x^2;

Now notice that the eval  command, which is supposed to do full evaluation, uses last name evaluation.

 > eval( f );

In this example, last name evaluation is the same as one level of evaluation.

 > eval( f, 1 );

We can get eval  to do full evaluation by forcing two levels of evaluation.

 > eval( f, 2 );

 >

 >

 >