in reply to Re: How am i doing?
in thread How am i doing?

Please note that Perl has a default warning if the recursion level exceeds 100 (it's configurable somewhere)
:/tmp$ perl -wE'my $x=0; sub rec { return if ($x++ >= 99); rec()}; rec +()' Deep recursion on subroutine "main::rec" at -e line 1. + # <--- :/tmp$ perl -wE'my $x=0; sub rec { return if ($x++ >= 98); rec()}; rec +()'

It's true that every recursion can be written with a loop (and more importantly vice versa!).

But recursions provide very clean maintainable code for many problems and in my experience rarely exceed the 100 threshold, where the memory load is negligible.

FWIW Larry provided us with goto &sub syntax which allows to (re)call a sub without creating a frame and pushing data on the stack.

Most importantly:

Fibonacci is the schoolbook example for with recursive calls and you are WAYYYYYY OFF the mainstream here.

Even flatearthers might ridicule you as a weird outsider.

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^3: How am i doing?
by choroba (Cardinal) on Jul 21, 2025 at 08:07 UTC
    > Fibonacci is the schoolbook example for with recursive calls

    Are you sure? I'm not sure what you mean exactly because of all the prepositions, but implementing the Fibonacci sequence by recursion is very ineffective inefficient. It's a schoolbook example of memoisation and implementing a recursive formula without recursion, I'd say.

    I agree with the rest of your node, though.

    Updated: Thanks LanX.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      ChatGPT agrees, HOW CAN I BE WRONG? ;-P

      Yes I'm sure, because I've seen it multiple times used this way, IIRC also in HOP.

      The F(n) = F(n-1) + F(n-2) for n > 1 part is straightforwardly implemented with recursion.

      > implementing the Fibonacci sequence by recursion is very ineffective.

      (Slightly nitpicking) It's inefficient but effective!

      The point is you get a correct result, even if you waste processing power.

      The "memoisation" you mentioned solves this efficiency problem of needlessly recalculating known results.

      IOW, the schoolbook continues evolving the example to deeper depth (sic).

      Keep in mind that the classic MIT lectures on programming used to be in Lisp and original Lisp didn't have loops, only those "tail recursions".

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

        Yes, you do recall correctly. From section 1.8 of HOP (page 33 in my version):

        Sometimes a problem appears to be naturally recursive, and then the recursive solution is grossly inefficient. A very simple example arises when you want to compute Fibonacci numbers. This is a rather unrealistic example, but it has the benefit of being very simple.

        🦛

Re^3: How am i doing?
by LanX (Saint) on Jul 29, 2025 at 19:25 UTC
    > (it's configurable somewhere)

    Unfortunately it's not, one has to recompile Perl to change that. Mea culpa!

    As a "funny" tangent, I asked ChatGPT and it hallucinated a special variable ${^RECURSION_LIMIT} ... LOL (sic)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery