Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A tail recursive function is a function where the only recursive call is the last one in the function. A non-tail recursive function is a function where that is not the case.</p> <p>A backward recursion is a recursion where in each recursive call the value of the parameter is less than in the previous step. A forward recursion is a recursion where it grows bigger with each step.</p> <p>Those are two orthogonal concepts, i.e. a forward recursion may or may not be tail-recursive and the same applies to backward recursions.</p> <p>For example the factorial function is often written like this in imperative languages:</p> <pre><code>fac = 1 for i from 1 to n: fac := fac * i </code></pre> <p>The common recursive version of factorial counts backwards (i.e. it calls itself with <code>n-1</code> as the parameter), however if you'd directly translate the above imperative solution, you'd come up with a recursive version that counts upwards. It would look something like this:</p> <pre><code>let fac n = let rec loop i = if i &gt;= n then i else i * loop (i+1) in loop 1 </code></pre> <p>This is a forward recursion and as you can see it is slightly more cumbersome than the backward recursive variant as it requires a helper function. Now this is not tail recursive as the last call in <code>loop</code> is the multiplication, not the recursion. So to make it tail-recursive, you'd do something like this:</p> <pre><code>let fac n = let rec loop acc i = if i &gt;= n then acc else loop (i*acc) (i+1) in loop 1 1 </code></pre> <p>Now this is both a forward recursion and a tail recursion because the recursive call is a) a tail-call and b) calls itself with a greater value (<code>i+1</code>).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload