logoalt Hacker News

Ar-Curunirtoday at 11:26 AM1 replyview on HN

Plenty of languages, including very serious ones like C and Rust, have bounded recursion depth.


Replies

mrkeentoday at 12:53 PM

Then let me rephrase:

If every iteration of a while-loop cost you a whole stack frame, then I'd be very rude about that language.

This works, btw:

  #include <stdio.h>

  long calc_sum(int n, long acc) {
    return n == 0
      ? acc
      : calc_sum(n-1, acc+n);
  }

  int main(void) {
    int iters = 2000000;
    printf("Sum 1...%d = %ld\n", iters, calc_sum(iters, 0));
    return 0;
  }