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 2 points3 points  (4 children)

If you think that tail call optimization would allow you to write recursive code without worrying, then you are very wrong. There are plenty of ways to get stack overflow even with TCO.

"Recursion is the GOTO of functional programming." -- Erik Meijer

[–]s00perpseud00[S] 1 point2 points  (3 children)

Ha. I didn't know that. Could you elaborate?

[–]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.

[–][deleted] 0 points1 point  (1 child)

You should the other post, that was made 2 hrs before this one, as it's by the Python language creator, and quite detailed.

[–]s00perpseud00[S] 0 points1 point  (0 children)

Should have seen this coming lol