Consider a function SqSub1 that decreases the argument by one and squares
the number obtained:
SqSub1(X) = (X-1)*(X-1)
This function can be defined in Refal in the following way:
$func SqSub1 sX = sZ;
SqSub1 sX = <Mult <Sub sX 1> <Sub sX 1>>;
An obvious deficiency of this definition is that it involves duplicate calculations: the
expression <Sub sX 1> is to be evaluated twice. But this can be
avoided by introducing an auxiliary function Sq:
$func SqSub1 sX = sZ;
$func Sq sY = sZ;
SqSub1 sX = <Sq <Sub sX 1>>;
Sq sY = <Mult sY sY>;
The function Sq serves the only purpose: it waits for the argument to be
decremented by one, catches the result obtained, and continues the computation. It is obvious
that superfluous auxiliary functions can make the program obscure and difficult to understand,
for which reason Refal Plus enables us to introduce local variables for denoting intermediate
values.
Namely, the definition of the function SqSub1 can be written in the
following way:
$func SqSub1 sX = sZ;
SqSub1 sX =
<Sub sX 1> :: sY,
<Mult sY sY>;
where <Sub sX 1> :: sY means that the variable
sY is to be bound to the result of evaluating <Sub sX
1>. Then <Mult sY sY> is evaluated, and the
result obtained is considered to be the result of evaluating the whole right hand side of the
sentence. It should be noted that the value of sY is used while evaluating
<Mult sY sY>.