<< Prev | - Up - | Next >> |
Boolean conditionals distinguish cases on basis of a Boolean value. Their syntax has the form if ... then ... else ... end
. We can use Boolean conditionals for instance to define functions for type coercion. The following procedure coerces a number (an integer or a float) into a float.
declare fun{NumberToFloat N}
if {IsFloat N}
then N
else {IntToFloat N}
end
end
%% summation of floats to integers requires type conversion
{Inspect {NumberToFloat 3.3}+{NumberToFloat 2}}
Boolean conditionals to define recursive functions on integers. Consider for instance the factorial function. We implement it by using the equality tester ==
of type .
%% {Factorial N} = N * ... * 1
declare fun{Factorial N}
if N==0
then 1
else N*{Factorial N-1}
end
end
{Inspect {Factorial 3}}
{Inspect {Factorial 1111}}
<< Prev | - Up - | Next >> |