On Mon, Feb 10, 2025 at 2:49 AM Alexander Burger <picolisp@software-lab.de> wrote:
> I'd like to point out, however, that recursion and tail call > optimization are not fully equivalent. We always need to keep that in > mind! > > ... > So what PicoLisp does is first clean up everything, by first *leaving* > all enclosing expressions, and then jumping to the start of the loop. > > ... > If we reduce the example to just a 'finally': > > (de f (N) > (finally (printsp N) > (if (=0 N) > (printsp 'OK) > (f (dec N)) ) ) ) > > : (f 3) > OK 0 1 2 3 -> OK > > Now, if we use 'tco' and 'tc' instead of the recursive call: > > (de f (N) > (tco (N) > (finally (printsp N) > (if (=0 N) > (printsp 'OK) > (tc (dec N)) ) ) ) ) > > : (f 3) > 3 2 1 OK 0 -> OK > > the result is completely different! > > Thanks for the insight! This is interesting and really helps my understanding of the mechanics of how tc is implemented in picolisp. /Lindsay