Application Center - Maplesoft

App Preview:

Maple Programming: 1.6: Anonymous functions and expressions

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

Learn about Maple
Download Application



Programming in Maple

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

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.

>    plot( z->z^2*sin(z)-1, -2..2 );

[Maple Plot]

>    plot( w^2/(1+exp(w)), w = -1..10 );

[Maple Plot]

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.

>    x -> x^3 + 2*x;

proc (x) options operator, arrow; x^3+2*x end proc

>    %(2);


>    D( %% );

proc (x) options operator, arrow; 3*x^2+2 end proc

>    int( (%%%)(x), x );


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.

>    z -> z/sqrt(1-z);

proc (z) options operator, arrow; z/sqrt(1-z) end proc

Now let us give the function a name, so that it is no longer anonymous.

>    f := %;

f := proc (z) options operator, arrow; z/sqrt(1-z) end proc

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 .

>    g := (x -> x) * sin;

g := proc (x) options operator, arrow; x end proc*sin

Here is another way to define the same function, this time using the anonymous expression x*sin(x) .

>    g := x -> x * sin(x);

g := proc (x) options operator, arrow; x*sin(x) end proc


Exercise : Here is a little puzzle. Consider the following command.

>    g := x -> x * sin;

g := proc (x) options operator, arrow; x*sin end proc

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.

>    f := ((x,y) -> x^2) + ((x,y) -> y^3);

f := proc (x, y) options operator, arrow; x^2 end proc+proc (x, y) options operator, arrow; y^3 end proc

>    f(u,v);


The following command is of course a much better way to define f .

>    f := (x,y) -> x^2 + y^3;

f := proc (x, y) options operator, arrow; x^2+y^3 end proc

Now compare f  with the next example, g , which uses two anonymous functions and defines a function of one  variable (not two).

>    g := (x -> x^2) + (y -> y^3);

g := proc (x) options operator, arrow; x^2 end proc+proc (y) options operator, arrow; y^3 end proc

>    g(u);


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?

>    k := (z -> z^2) + ((x,y) -> sin(x*y));

k := proc (z) options operator, arrow; z^2 end proc+proc (x, y) options operator, arrow; sin(x*y) end proc


The next command defines an anonymous function and evaluates it right away.

>    (z -> z^2*tan(z))(Pi/4);


Why should the next command be considered an anonymous expression?

>    (z -> z^2*tan(z))(w);



Exercise : Explain what the following command is doing.

>    (D(z->3*z^2-2*x+z))(3);


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 .

>    f := x^2*sin(1/x);;

f := x^2*sin(1/x)

Why should the following be considered an anonymous function?

>    unapply( f, x );

proc (x) options operator, arrow; x^2*sin(1/x) end proc


Exercise : Why should the following be considered an anonymous function?

>    exp*sin;


Define this function in at least two other ways.


Exercise : Is the following an anonymous function?

>    x -> sin(x);



Exercise : The next command uses an anonymous expression to create a sequence.

>    seq( 3*n-2, n=1..10 );

1, 4, 7, 10, 13, 16, 19, 22, 25, 28

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?

>    plot( ((x,y)->x^3-y^3)(w,-1), w = -1..1 );

[Maple Plot]

>    plot( w->(((x,y)->x^3-y^3)(w,-1)), -1..1 );

[Maple Plot]


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.)

>    plot( (z->z^2+1)(y), z=-3..3 );

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.