This is just the Python version of https://ivmoreau.com/rust-tail-cail-functions-i/
It's horrible, and I love it. (I also did a pseudo-match "expression")
class TCO: pass class Ret(TCO): def __init__(self, value): self.v = value class Rec(TCO): def __init__(self, *args): self.v = args def tco(f): def g(*args): c = f(*args) while True: if isinstance(c, Ret): return c.v else: c = f(*(c.v)) return g def match(x): def g(*funs): for (cmp, fun) in funs: if cmp == x: return fun() return g @tco def fib(c, i, j): return match(c)( (0, lambda: Ret(i)), (1, lambda: Ret(j)), (2, lambda: Ret(i + j)), (c, lambda: Rec((c - 1, j, i + j))), ) print(fib(1000000, 0, 1))
Yes, this somehow works, though I don't understand Python tuple destructuring (the v part) all that well.