functional programming - tail recursion vs. forward recursion -


Can someone give me a distinction between these two types of recycling and examples (especially OCaml)? A tail recursive function is a function where only the recursive call in the function is final. A non-tail recurring function is a function where there is no such case.

A backward recurrence is a recursive, where the value of the parameter in each recurring call is less than the previous step. A next recurrence is a recursive, where it grows with each stage.

These are two orthogonal concepts, that is, whether a next recurrence can be tail-recursive, it applies to backward recurrence.

For example, factual work in important languages ​​is often written like this:

  from FC = 1 to 1 n: FAC: FAC = I  

The usual recurring version of the genre (i.e., it calls itself as a parameter in the form of n-1 ), although if you directly mentioned above Translate the solution, then you will come with a recurring version which is counted upward Something like this looks:

  let's F = NER loop I = if i> gt; = N Then and again i i loop 1 in i * loop  

this is a next recursion and as you can see it is slightly more than the backward recursive type It is cumbersome because it requires an assistant function. Now this is not a recursive tail because the last call in loop is multiplied, not recursive. So to make it a tail-recursive, you want to do something like this:

  Let's focus N = REC loop ACC i = if i & gt; = N Then another AC loop (i * ACC) (1 + 1) in loop 1 1  

Now this is both a next recurring and a tail calculation because the recursive call is one) tail call And b) give yourself a higher price ( i + 1 ).


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -