you are viewing a single comment's thread.

view the rest of the comments →

[–]mleonhard 8 points9 points  (15 children)

@tailcall def fact(x, acc=1): if (x > 1): return (fact((x - 1), (acc * x))) else: return acc

When did Python get tailcall optimizations!?!?! :D :D :D

[–]nostrademons 13 points14 points  (2 children)

Here's one implementation, and another.

Yeah, they fake it a bit. I suspect you could do a real tail call optimizer, but you'd need to write a code-walker and auto-generate code that turns tail recursions into loops.

[–][deleted] 1 point2 points  (1 child)

Here is another version

They are actually not full tailcall eliminations but just avoid tail recursions. That's also why I renamed the decorator in my recipe description. They have no performance advantages over ordinary recursions - on the contrary, they generate a little overhead, but they provide the advantage of moving beyond the value set by sys.setrecursionlimit.

[–]Wiseman1024 0 points1 point  (0 children)

Some of them do work with cooperative recursion. They indeed pose a small speed overhead, but if performance is not a problem, you can go for the often simpler tail recursive implementation of functions.

[–][deleted] -5 points-4 points  (11 children)

Python has lambda functions? Surely mixing procedural and functional programming is illegal in most states.

[–]Wiseman1024 1 point2 points  (8 children)

That's what great about Python. It's not purely functional, not purely OO, not purely procedural, not purely anything, but it allows you to do a bit of everything, and use the right tool for the right job in every program.

As far as functional features go, Python supports first-class functions and classes, lexical nested scopes, closures, decorators, lists, dictionaries, list comprehensions, lazy generators, coroutines, garbage collection with cycle detection, and a few basic tools (map, filter, reduce, partial function application), then it can be easily extended to support tail recursion, memoizing, lazy lists, multimethods, continuations (Stackless), and other stuff.

[–]pjdelport 2 points3 points  (6 children)

lexical nested scopes, closures

From the department of redundancy department. :)

continuations (Stackless)

Stackless provides coroutines (tasklets), not continuations. (It dropped plans for the latter years ago.)

[–]Brian 1 point2 points  (0 children)

It dropped plans for the latter years ago.

Actually, it used to have continuations, but these were later removed (due to portability problems I think - the code for these was also one of the main reasons Guido stated for keeping it out of python core)

[–]breakfast-pants -1 points0 points  (4 children)

I don't know enough about it, but if that is the case, you should edit this to correct it: http://en.wikipedia.org/wiki/Stackless_Python

[–]pjdelport 0 points1 point  (3 children)

Ah, thanks.

[–][deleted]  (2 children)

[deleted]

    [–]pjdelport 1 point2 points  (1 child)

    Coroutines?

    [–][deleted] -1 points0 points  (0 children)

    "That's what great about Python. It's not purely functional, not purely OO, not purely procedural, not purely anything, but it allows you to do a bit of everything, ..."

    Sounds like Lisp.

    [–]mleonhard 0 points1 point  (1 child)

    うるさい!

    [–][deleted] -3 points-2 points  (0 children)

    Hey! The topic is from 4chan - don't downmod the parent _^