资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

如何在JavaScript,Scala和ABAP里实现尾递

在JavaScript, Scala和ABAP里实现尾递归(Tail Recursion),针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

创新互联是专业的阜阳网站建设公司,阜阳接单;提供成都网站建设、成都做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行阜阳网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

Before we start to research tail recursion, let’s first have a look at the normal recursion.

A simple factorial implementation by recursion:

function factorial(n){
  if(n ===1) {
     return 1;
  }
  return n *factorial(n -1);}

Let N = 5, see how new stack frame is created for each time of recursive call:

如何在JavaScript, Scala和ABAP里实现尾递

We have two stack frames now, one stores the context when n = 5, and the topmost one for current calculation: n = 4

如何在JavaScript, Scala和ABAP里实现尾递

如何在JavaScript, Scala和ABAP里实现尾递

如何在JavaScript, Scala和ABAP里实现尾递

Now since n equals to 1, we stop recursion. The current stack frame ( n = 1 ) will be poped up, the frame under it will be activated and become the topmost frame, with calculated result 1 passed into.

如何在JavaScript, Scala和ABAP里实现尾递

如何在JavaScript, Scala和ABAP里实现尾递

如何在JavaScript, Scala和ABAP里实现尾递

如何在JavaScript, Scala和ABAP里实现尾递

如何在JavaScript, Scala和ABAP里实现尾递

key note for normal recursion: during recursion, every generated stack frame is needed and could not e destroyed until the final result is calculated. The calculation is actually not started until the recursion reaches the end ( the condition n === 1 fulfills ). If N is a big integer, it will lead to huge number of stack frames and finally the “stack overflow” or “out of memory” is inevitable.

tail recursion

Source code below:

function tailFactorial(n, total) {if(n ===1)
    return total;return tailFactorial(n -1, n * total);}function factorial2(n) {
  return tailFactorial(n,1);}

There are two biggest differences compared with normal recursion:

(1) A new internal function tailFactorial is introduced here.

(2) The calculation is actually now spread within every recursive stack frame. Each frame finishes one part of calculation and pass the current result to the next frame. Once the current stack frame finishes its task, it is actually not needed any more. And thus for example the model browser can then do some optimization on those useless stack frames.

Observe the stack frame for tail recursion step by step:

如何在JavaScript, Scala和ABAP里实现尾递

如何在JavaScript, Scala和ABAP里实现尾递

如何在JavaScript, Scala和ABAP里实现尾递

如何在JavaScript, Scala和ABAP里实现尾递

如何在JavaScript, Scala和ABAP里实现尾递

如何在JavaScript, Scala和ABAP里实现尾递

如何在JavaScript, Scala和ABAP里实现尾递

stack popped up:

如何在JavaScript, Scala和ABAP里实现尾递

When N = 20, the tail recursion has a far better performance than the normal recursion:

如何在JavaScript, Scala和ABAP里实现尾递

Update 2016-01-11

Tail recursion implementation via Scala:

如何在JavaScript, Scala和ABAP里实现尾递

The interesting thing is, after the Scala code is compiled into Java Byte code, compiler will eliminate the recursion automatically:

如何在JavaScript, Scala和ABAP里实现尾递

Tail Recursion in ABAP

First this is the normal recursion:

REPORT zrecursion.
START-OF-SELECTION.
  DATA: lv_result TYPE int4.
  PERFORM fac USING 6 CHANGING lv_result.
  WRITE: / lv_result.
FORM fac USING iv_n TYPE int4 CHANGING cv_result TYPE int4.
  DATA: lv_n TYPE i.
  cv_result = lv_n = iv_n.
  lv_n = lv_n - 1.
  IF lv_n > 1.
    PERFORM fac USING lv_n CHANGING cv_result.
  ENDIF.
  IF lv_n = 1.
    cv_result = cv_result * lv_n.
  ELSE.
    cv_result = cv_result * iv_n.
  ENDIF.
ENDFORM.

And here comes tail recursion version:

REPORT ztail.
START-OF-SELECTION.
  DATA: lv_result TYPE int4.
  PERFORM fac USING 5 1 CHANGING lv_result.
  WRITE: / lv_result.
FORM fac USING iv_n TYPE int4 iv_acc TYPE int4 CHANGING cv_result TYPE int4.
  DATA: lv_n          TYPE i,
        lv_accumulate TYPE i.
  IF iv_n < 1.
    cv_result = 1.
  ELSEIF iv_n = 1.
    cv_result = iv_acc * iv_n.
  ELSEIF iv_n > 1.
    lv_n = iv_n - 1.
    lv_accumulate = iv_acc * iv_n.
    PERFORM fac USING lv_n lv_accumulate CHANGING cv_result.
  ENDIF.
ENDFORM.

关于在JavaScript, Scala和ABAP里实现尾递归(Tail Recursion)问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


分享名称:如何在JavaScript,Scala和ABAP里实现尾递
文章URL:http://cdkjz.cn/article/pspchc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220