Newsgroups: comp.sys.transputer
From: Richard Beton <Richard.Beton@roke.co.uk>
Subject: Re: Is Occam a recursive language ?
Organization: Roke Manor Research Limited
Date: Tue, 24 Jun 1997 08:16:47 +0100
Mime-Version: 1.0
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-ID: <33AF745F.1841CC22@roke.co.uk>

<HTML>
egamess@kanaima.ciens.ucv.ve wrote:
<BLOCKQUOTE TYPE=CITE>I am a new programmer in Occam. Is Occam is a recursive
language ?
<BR>If yes, can someone write the factorial function ?

<P>Thank you very much for help.</BLOCKQUOTE>
&nbsp; Occam deliberately prevents the expression of recursion. It does
this by requiring all identifiers to be in scope at the point they are
used, and since it does not have syntax for declaring forward references,
it is not possible to write recursive programs.

<P>Why does occam not allow recursion? The primary reason is that occam
gains many of its benefits for embedded programming from the compiler's
ability to compute (at compile time) exact memory sizes for stacks etc.
You know exactly how much memory a given occam program will ever require,
because the compiler can tell you that information. The difficulty with
recursion is computing the stack size required - this is not possible in
the general case (similarly, it is not possible, in general, to confirm
that programs containing WHILE loops will ever terminate). Therefore occam
does not allow recursion.

<P>There exist mappings for all real recursive programs such that the recursion
is replaced by iteration. It then falls to the programmer to provide an
array for the data structures which, in the recursive version, would be
provided by the compiler using the stack.

<P>Often, however, converting a recursive algorithm to an iterative one
actually involves a different algorithm, for the sake of simplicity, transparency
and efficiency.

<P>Example: factorial expressed using (illegal) recursion:

<P><TT>INT FUNCTION factorial (VAL INT n)</TT>
<BR><TT>&nbsp; INT result:</TT>
<BR><TT>&nbsp; VALOF</TT>
<BR><TT>&nbsp;&nbsp;&nbsp; IF</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (n > 1)</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result := n * factorial
(n-1)</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (TRUE)</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result := 1</TT>
<BR><TT>&nbsp;&nbsp;&nbsp; RESULT result</TT>
<BR><TT>:</TT>

<P>Example: factorial expressed using iteration:

<P><TT>INT FUNCTION factorial (VAL INT n)</TT>
<BR><TT>&nbsp; INITIAL INT result IS 1:</TT>
<BR><TT>&nbsp; VALOF</TT>
<BR><TT>&nbsp;&nbsp;&nbsp; INITIAL INT x IS n:</TT>
<BR><TT>&nbsp;&nbsp;&nbsp; WHILE (x > 1)</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SEQ</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result := result * x</TT>
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x := x - 1</TT>
<BR><TT>&nbsp;&nbsp;&nbsp; RESULT result</TT>
<BR><TT>:</TT>

<P>Recursion is a concept drilled into mathematics at a very fundamental
level. Consequently, it seems hard at first not to use it. It is often
the natural way to express an algorithm. However, I have often found iterative
algorithms with which I have replaced recursion to be more straightforward
and less messy than I would have expected: my experience is that not having
recursion has not been a problem. The iterative factorial function (above)
illustrates how the program size and complexity do not necessarily increase,
whilst the memory requirement becomes fixed at compile time. The iterative
factorial would probably execute faster, too.

<P>Regards,
<BR>Rick
<BR>--
<BR>Richard Beton BSc CPhys MInstP
<BR>Roke Manor Research Limited
<BR>--------- Standard Disclaimer about my own views etc etc --------
<BR>Welsh Highland Railway: <A HREF="http://www.whr.co.uk/WHR/WHR.html">http://www.whr.co.uk/WHR/WHR.html</A>
<BR>&nbsp;</HTML>


