Tail recursion

(Difference between revisions)
 Revision as of 09:13, 23 May 2010 (edit) (Make 'foldl' the object of the link, and move below quote from Brent.)← Previous diff Revision as of 00:09, 16 June 2010 (edit) (undo) (formal definition. vague removed.)Next diff → Line 5: Line 5: not tail recursive. not tail recursive. - With that said, tail recursion is not that useful of a concept in a + Here is formal definition of "tail recursive". "f occurs in t" means f is a free variable of t. - lazy language like Haskell. The important concept to know in Haskell + - is [[guarded recursion]], where any recursive calls occur within a data + - constructor (such as foldr, where the recursive call to foldr occurs + - as an argument to (:)). This allows the result of the function to be + - consumed lazily, since it can be evaluated up to the data constructor + - and the recursive call delayed until needed. + - Note that [[Fold|foldl]] is always tail recursive. + When a function is defined (in let or at the top level) as: + f = t + where f is a name and t is a lambda-term, f is ''tail recursive'' iff f occurs tail recursively in t. f ''occurs tail recursively'' in t iff f occurs in t and any of the following holds: + * t is variable; + * t is "\var -> t0" and f occurs tail recursively in t0; + * t is "t0 t1" and f occurs tail recursively in t0 and does not occur in t1; + * t is "let bs in t0" and f occurs tail recursively in t0 and for each binder "var = t1" in bs, f does not occur in t1; + * t is "case t0 of bs" and f does not occur in t0 and for each branch b in bs, f does not occur or occurs tail recursively in b; + ** when we are saying "occur in b", b has form "D vars -> t" (where D is some data constructor and vars is a sequence of names), we are thinking of the lambda-abstraction "\vars -> t" instead of b. + + Note that [[Fold|foldl]] is tail recursive. + + The important concept to know in Haskell is [[guarded recursion]], where any recursive calls occur within a data constructor (such as foldr, where the recursive call to foldr occurs as an argument to (:)). This allows the result of the function to be consumed lazily, since it can be evaluated up to the data constructor and the recursive call delayed until needed. == Source == == Source ==

Revision as of 00:09, 16 June 2010

A recursive function is tail recursive if the final result of the recursive call is the final result of the function itself. If the result of the recursive call must be further processed (say, by adding 1 to it, or consing another element onto the beginning of it), it is not tail recursive.

Here is formal definition of "tail recursive". "`f` occurs in `t`" means `f` is a free variable of `t`.

When a function is defined (in `let` or at the top level) as:

``` f = t
```

where `f` is a name and `t` is a lambda-term, `f` is tail recursive iff `f` occurs tail recursively in `t`. `f` occurs tail recursively in `t` iff `f` occurs in `t` and any of the following holds:

• `t` is variable;
• `t` is "`\var -> t0`" and `f` occurs tail recursively in `t0`;
• `t` is "`t0 t1`" and `f` occurs tail recursively in `t0` and does not occur in `t1`;
• `t` is "`let bs in t0`" and `f` occurs tail recursively in `t0` and for each binder "`var = t1`" in `bs`, `f` does not occur in `t1`;
• `t` is "`case t0 of bs`" and `f` does not occur in `t0` and for each branch `b` in `bs`, `f` does not occur or occurs tail recursively in `b`;
• when we are saying "occur in `b`", `b` has form "`D vars -> t`" (where `D` is some data constructor and `vars` is a sequence of names), we are thinking of the lambda-abstraction "`\vars -> t`" instead of `b`.

Note that foldl is tail recursive.

The important concept to know in Haskell is guarded recursion, where any recursive calls occur within a data constructor (such as
foldr
, where the recursive call to foldr occurs as an argument to
(:)
). This allows the result of the function to be consumed lazily, since it can be evaluated up to the data constructor and the recursive call delayed until needed.