Tail Recursion With Common Lisp, Do or Don't?

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 พ.ย. 2024

ความคิดเห็น • 10

  • @colinwoodbury6430
    @colinwoodbury6430 9 หลายเดือนก่อน +4

    I love using `labels`. For people used to Clojure, it allows a similar pattern as loop/recur. From my own testing, `labels` appears to do proper Tail Call Elimination for (at least) SBCL, ECL, CCL, Clasp, and LW8. As you mentioned with ABCL, it also doesn't seem to do TCE for `labels` either, unfortunately.

    • @the-lisper
      @the-lisper  9 หลายเดือนก่อน +1

      Thanks for sharing your experience!

    • @jongeduard
      @jongeduard 9 หลายเดือนก่อน

      Looks like kind of a functional version of the C style goto concept. It performs a jump, but it still behaves like a function. Fascinating.

    • @the-lisper
      @the-lisper  9 หลายเดือนก่อน +1

      @@jongeduard Behind the scene every loop/iteration (for, while, do ... while) is a goto! TCE is really changing recursion in iteration automatically!

    • @jongeduard
      @jongeduard 9 หลายเดือนก่อน +1

      @@the-lisper True, ASM all just jumps. I have done some testing with the tail recursion in the Rust programming language too. Rust is very strict and clear about safety and reliability of everything. The docs clearly explain how tail recursion is also not guaranteed at all and should not be relied on.
      However, when I do very simple test with it, it currently does only stack overflow using a Debug build and not in the Release build, where it just goes infinite and 100 percent on a CPU thread without any crash.
      And this also fits into the more general zero cost philosophy or Rust, where it basically inlines and eliminates most functions and other abstractions. Which is why it runs so incredibly fast.

    • @georgerogers1166
      @georgerogers1166 6 หลายเดือนก่อน

      Did the loop heap allocate? Rust's memory model prevents tail calls in certain cases.

  • @serenditymuse
    @serenditymuse 19 วันที่ผ่านมา

    Definitely do. This is Lisp. It should support anything that is useful to do.

    • @the-lisper
      @the-lisper  19 วันที่ผ่านมา

      Ja, the ANSI standard just doesn't mandate to optimize it. Even though it usually does (when possible)

  • @jongeduard
    @jongeduard 9 หลายเดือนก่อน +1

    Thanks for this video. I Just started exploring Lisp a bit, out of fascination, and because it is an influence on some modern languages. Rust has inspiration out of it for example.
    I don't develop in pure functional languages (yet), but as far as I know is a general solution is to use recursive higher order functions like fold and reduce.
    At least from reduce I know that Lisp has that one as well.
    Obviously in fact this also shifts the problem. How are these functions implemented? Well probably with iteration. But I guess these functions are kind of made intrinsic in pure languages like Haskell. So that these details are more hidden to the eye.

    • @the-lisper
      @the-lisper  9 หลายเดือนก่อน +1

      Yes, it is common practise to use high level functions like map/reduce/filter/.... but what if you can't?
      Recursion and iteration are more generic concept, you can build the high level functions using them. These functions don't have to be intrisic.