947
you are viewing a single comment's thread
view the rest of the comments
[-] nothacking@discuss.tchncs.de 76 points 9 months ago

GNU is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX is not UNIX[Maximum call stack size exceeded]

[-] Klear@lemmy.world 21 points 9 months ago

GNU is not UNiplexed Information Computing System is not UNiplexed Information Computing System is not UNiplexed Information Computing System is not UNiplexed Information Computing System is not UNiplexed Information Computing System is not UNiplexed Information Computing System is not UNiplexed Information Computing System is not UNiplexed Information Computing System is not UNiplexed Information Computing System is not UNiplexed Information Computing System is not UNiplexed Information Computing System is not UNiplexed Information Computing System is not UNiplexed Information Computing System is not UNiplexed Information Computing System is not UNiplexed Information Computing System is not UNiplexed Information Computing System

[-] SexualPolytope@lemmy.sdf.org 8 points 9 months ago

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";;
[-] barsoap@lemm.ee 8 points 9 months ago* (last edited 9 months ago)

Not an OCaml expert either but that looks tail recursive, you're never going to blow the stack.

You can tell by how after the recursive call within aux, its result does not get used within the function. That means that the compiler doesn't need to push a return address to the stack as the only code that would be at that address is instructions to pop another address and return there, we can short-circuit all that and jump from the base case (0) directly to where aux(10000) is supposed to return to instead of taking 10000 dumb steps (like practically all procedural languages do because they don't have tail call optimisation).

This would've been different if you had concatenated the string not as an argument to aux.

[-] sacredfire@programming.dev 3 points 9 months ago* (last edited 9 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.

[-] barsoap@lemm.ee 3 points 9 months ago

Yes you still need the loop part I skipped over that one, only focussing on the "why no return address on the stack" part. It's what you need to focus on to see whether a recursive call is in a tail position and if it is the compiler does the rest no need to worry about that part.

[-] SexualPolytope@lemmy.sdf.org 2 points 9 months ago* (last edited 9 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.

[-] barsoap@lemm.ee 5 points 9 months ago

OCaml certainly isn't a bad language to learn for a non-professional. It's almost painfully sensible and well-engineered, you're far away from hype train nonsense and startup production jank but also not out in the "the purpose of this language is to be beautiful and earn me a PhD" territory, OCaml definitely is a production language.

this post was submitted on 04 Mar 2024
947 points (97.4% liked)

Programmer Humor

32692 readers
443 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS