• A tail-recursive version written in OCaml that should not reach stack limits easily. (Not an expert in OCaml, so this might be stupid. But I tried it with 10000 iterations, and it worked without any issues.)

    let gnu =
        let rec aux s = function
        | 0 -> s
        | n -> aux (s^" is Not Unix") (n-1)
    in aux "GNU";;
    
      • sacredfire@programming.dev
        link
        fedilink
        arrow-up
        3
        ·
        edit-2
        11 months ago

        I thought Tail recursion just gets turned into an iterative loop by the compiler? Hence why you won’t get a stack overflow. And since in procedural languages you can just use a loop in place of a tail recursive function you would never run into this problem, right? At least this is how it was taught to me when I was learning about it in lisp.

      • ѕєχυαℓ ρσℓутσρє@lemmy.sdf.org
        link
        fedilink
        arrow-up
        2
        ·
        edit-2
        11 months ago

        That was the idea. But I’m not a functional programmer (not a programmer by profession at all lol), so I might’ve done something stupid. Hence the disclaimer. Thanks for confirming.