Python で
def tail(fun, *args): return (fun, args) def call_with_tco(fun, *args): esc = args[-1] c = (fun, args) while c[0] != esc: c = c[0](*c[1]) return c[0](*c[1]) def identity(x): return x def fact(n, acc, k): if n > 0: return tail(fact, n - 1, acc * n, k) else: return tail(k, acc) print(call_with_tco(fact, 5, 1, identity))
ほぼ Scheme 版そのまま...JavaScript 版みたいにもできるかな?