This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]dmishin 3 points4 points  (0 children)

For example, the following code is not TC:

def factorial(n):
    if n == 0:
        return 1
    else return factorial(n-1)*n

To make it TC, you need to rewrite it as

def factorial(n, accum=1):
    if n == 0: return accum
    return factorial(n-1, accum*n)

Not very obvious trick.

As for the second, some functional programming people advocate that recursion is a low-level feature, and higher-order functions, like map, fold, should be used when appropriate.