What is tail recursion?

前端 未结 28 3709
一个人的身影
一个人的身影 2020-11-21 05:03

Whilst starting to learn lisp, I\'ve come across the term tail-recursive. What does it mean exactly?

28条回答
  •  星月不相逢
    2020-11-21 05:07

    here is a Perl 5 version of the tailrecsum function mentioned earlier.

    sub tail_rec_sum($;$){
      my( $x,$running_total ) = (@_,0);
    
      return $running_total unless $x;
    
      @_ = ($x-1,$running_total+$x);
      goto &tail_rec_sum; # throw away current stack frame
    }
    

提交回复
热议问题