Tail-Recursion

This function is recursive but not tail-recursive. The stack space it
consumes is proportional to the value of its argument. Essentially,
the ATS compiler translates the definition of
sum1 into the following C code:

The inner function loop in the definition of sum2 is
tail-recursive. The stack space consumed by loop is a constant
independent of th value of the argument of sum2. Essentially, the
ATS compiler translates the definition of sum2 into the following C
code:

Mutual Tail-Recursion

Sometimes, mutually tail-recursive functions are encountered. For
instance, in the following example, the functions even and
odd are mutually tail-recursive.

// [fn*] indicates the need to combine two or more functions
// so as to translate tail-recursive calls into direct jumps
fn* even (n:int):bool=if n > 0 then odd (n-1)else true
and odd (n:int):bool=if n > 0 then even (n-1)else false

The keyword fn* is used to indicate to the ATS compiler
that the functions even and
odd need to be combined together so as to turn (mutually)
recursive function calls into direct jumps. Essentially, the ATS compiler
emits the following C code after compiling this example: