Programming in Maple
Roger Kraft Department of Mathematics, Computer Science, and Statistics Purdue University Calumet
roger@calumet.purdue.edu
1.6. Anonymous functions and expressions (review)
We can give functions and expressions names, and this of course makes it easier to work with them. But we do not have to give a function or expression a name in order to work with it. For example, the following plot commands graph a function and an expression, neither of which is given a name.
There is a name for the concept of an "unnamed function or expression". We call a Maple function (or expression) that has not been assigned a name an anonymous function (or an anonymous expression ). The rest of this section is a number of examples and problems that make use of anonymous functions and expressions.
Here we define an anonymous function and then evaluate, differentiate, and integrate it.
Notice that we can no longer refer to our anonymous function. We have run out of ditto operators.
The next command defines another anonymous function.
Now let us give the function a name, so that it is no longer anonymous.
Notice that these last two commands show once again that defining a function and naming a function are two very distinct steps.
The next example defines a named function g using a (very simple) anonymous function x -> x and the built in function sin .
Here is another way to define the same function, this time using the anonymous expression x*sin(x) .
Exercise : Here is a little puzzle. Consider the following command.
Does this command make sense?
Here is a very awkward command that defines a function f of two variables as the sum of two anonymous functions.
The following command is of course a much better way to define f .
Now compare f with the next example, g , which uses two anonymous functions and defines a function of one variable (not two).
What would be a more straight forward way to define g ?
Exercise : Is the following function k ,defined using two anonymous functions, a function of one, two or three variables?
The next command defines an anonymous function and evaluates it right away.
Why should the next command be considered an anonymous expression?
Exercise : Explain what the following command is doing.
Translate the above command into a single, equivalent Maple command that uses an anonymous expression instead of an anonymous function.
Here is an expression named f .
Why should the following be considered an anonymous function?
Exercise : Why should the following be considered an anonymous function?
Define this function in at least two other ways.
Exercise : Is the following an anonymous function?
Exercise : The next command uses an anonymous expression to create a sequence.
Can you rewrite this command to somehow make use of an anonymous function?
Exercise : Explain in detail what the following two commands do and how they do it. How does each command makes use of anonymous functions and/or expressions?
Exercise : Explain what is wrong with the following plot command and then fix it by changing only one character in the command. (There are two ways to solve this problem.)
Plotting error, empty plot
Anonymous functions are very common in mathematics. For example, the following two formulas make use of them.
Mathematics textbooks are filled with formulas like these. We usually do not think of such formulas as using "anonymous functions". The concept of an anonymous function is not all that useful in most mathematics books. But as we will see, in Maple, and in computer programming in general, the concept of an anonymous function is very useful.