4.11 Boolean Conditionals

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 {\rm value} 
\times {\rm value} \rightarrow {\rm bool}.

%%    {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}}


Denys Duchier, Claire Gardent and Joachim Niehren
Version 1.3.99 (20050412)