Maple evalution rules at function definition

63 Views Asked by At

I am trying to define a function, A, which has a function call, B, in its body. The function call to B is independent of input parameters of A. Maple does not call B at function definition. Is there any way to force it to evaluate call to B at function definition time?
Example:

B := x -> 2 * x;
A := x -> B(2) + x ^ 2;

A first it might look irelevent to ask for compution a constant at function definition type, but it have some usage cases for example at higher order functions:

A := n -> (x-> add(i, i=1..n)*x)
2

There are 2 best solutions below

0
On BEST ANSWER
A:= subs(Z = B(2), (x -> Z + x^2)); 
2
On

While Roberts answer is good and general purpose, I'd mention that for simple 1-line procedure B there is another way which accomplishes the goal automatically.

It involves having option inline on B.

For example,

restart;

B := proc(x) option inline; 2 * x; end proc:

A := x -> B(2) + x ^ 2;

                       2
        A := x -> 4 + x

Or, if you want the body of B to still print with the arrow (though hiding its inline nature from overt view),

restart;

B := proc(x) option inline, operator, arrow; 2 * x; end proc;

        B := x -> 2 x

A := x -> B(2) + x ^ 2;

                       2
        A := x -> 4 + x