1

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 Answers2

2
A:= subs(Z = B(2), (x -> Z + x^2)); 
Robert Israel
  • 448,999
0

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
acer
  • 5,293
  • As I understood by specifying iniline option we tell Maple to treat B as arithmetic operators which it replace them with their results during function defention procces. Did I got it right? – S.Sadegh Marashi Nov 22 '17 at 15:42
  • There is no restriction of B to "arithmetic" operations. But, yes, the inline option on procedure B causes the call to B inside another procedure's definition to be replaced by the "results". – acer Nov 22 '17 at 15:51