Programming in Maple
Roger Kraft Department of Mathematics, Computer Science, and Statistics Purdue University Calumet
roger@calumet.purdue.edu
2.8. Evaluating function definitions
In the last section we emphasized how Maple evaluates function calls. Here we look at how Maple evaluates a function's definition. We will see that this also gives us more information about the evaluation of function calls.
Let us give x a value.
If we define f as an expression in x , then full evaluation will evaluate x right away and so x will not really be part of the value of f .
If we now change x , this does not affect f .
Now define g as a Maple function and use x as the independent variable in the definition of g .
Maple did not use full evaluation here. None of the x 's on the right hand side of the assignment operator were evaluated. If we evaluate a function call using g , then the x in the definition of g will get the operand of the function call, not the current value of x .
Notice that the current value of x (which is ) has nothing to do with the evaluation of g(6) .
Here is another example. Suppose we give the name c a value and then use it in the definition of g along with x .
Notice that neither c nor x was evaluated in the definition of g .
If we evaluate a function call using g , then Maple will use the current value of c in evaluating the function call but not the current value of x . This is because c is a parameter in the definition of g but x is the independent variable (and independent variables get their values from the function call's operands).
If we now change the value of c , then the definition of g is unchanged, but the evaluation of g in a function call will change.
We can even leave c unassigned.
So when Maple evaluates a function definition using the arrow operator, it does not evaluate any of the names in the definition, neither the independent variables nor the parameters. When Maple evaluates a function call, then the independent variables get their values from the operands of the function call, and the parameters in the function definition are fully evaluated to whatever values they may currently have at the time of the function call.
So far we have looked at how Maple evaluates a function definition that uses the arrow notation. But we can also define Maple functions using the unapply command. Let us see how Maple evaluates a function definition that uses the unapply command.
Error, (in unapply) variables must be unique and of type name
We got an error message. What happened is that Maple used full evaluation for the unapply command. So this last command looked to Maple like the following
because x has a value (and c does not).
Here is a way to verify this.
So the unapply command uses full evaluation. Let us see how that affects the function we are defining. First of all, we must have the independent variable (in this case x ) as an unassigned variable.
Now we can use unapply .
Notice that since c is currently unassigned, c is a parameter in the definition of g .
We can give c different values and get different evaluations of function calls to g .
What happens if we now use unapply again to redefine g using the same expression?
Now, since c is an assigned variable and Maple uses full evaluation in the unapply command, we no longer have c as a parameter in the definition of g . The value 2 is now a permanent part of the definition of g .
What if we want to use unapply and force c to be a parameter in the definition of g ? Then we would need to delay the evaluation of c in the unapply command.
Why is there a 5 in the last expression?
In summary, the arrow operator and the unassign command use very different evaluation rules when used to define a Maple function. The arrow operator does not evaluate any of the names used in the definition of the function, and the unapply command uses full evaluation (except in cases where last name evaluation should be used).
Exercise : Explain each the following three sequences of commands.
Exercise : Explain the following sequence of commands.
Since h has a value, why was there no error message from the last two commands?
For example, here we get an error message
which we get rid of this way.
Exercise : Explain with as much detail as you can how Maple evaluates the following commands. What rules of evaluation are used at each step.
How would we use unapply to define the exact same function g that we have right now?
The following is not correct. Try to fix it.
Explain what happens if you remove any one of the pairs of right-quotes from the unapply command. And why does the last h in the unapply command not even need right-quotes?