4 ms·
Correct me if I'm wrong, but I think it's not possible to express all recursion as tail-call recursion. So I think the statement is correct, in the general case
by losvedir 6y ago
Correct me if I'm wrong, but I think it's not possible to express all recursion as tail-call recursion. So I think the statement is correct, in the general case.
But, yes, it's true that you can express some recursion with fixed memory size. And, in fact, the @call[0] built-in has an `always_tail` which asserts that the call should always be generated with tail call recursion optimization, and is an error at compile time, if not.
You might also be interested in following this (open) GitHub issue[1] that explores recursion in more detail.
But, yes, this area of Zig seems to be still a little "experimental", as you say. (But shows great promise, I think!)
[0] https://ziglang.org/documentation/0.7.1/#call https://ziglang.org/documentation/0.7.1/#call
[1] https://github.com/ziglang/zig/issues/1006 https://github.com/ziglang/zig/issues/1006
- zelphirkalt 6y agoI think it is possible to express all recursion as tail-call recursion, but only by passing continuations as arguments, which might lose the advantage of a tail-call recursion. Any computation you have left in the current frame you could put into a continuation. However, that continuation then grows, so you need more memory for it, instead of multiple stack frames, so the advantage ist lost.