JavaScript和ABAP的尾遞迴
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:
We have two stack frames now, one stores the context when n = 5, and the topmost one for current calculation: n = 4
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.
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:
stack popped up:
When N = 20, the tail recursion has a far better performance than the normal recursion:
Update 2016-01-11
Tail recursion implementation via Scala:
The interesting thing is, after the Scala code is compiled into Java Byte code, compiler will eliminate the recursion automatically:
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.
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2719289/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- JavaScript, ABAP和Scala裡的尾遞迴(Tail Recursion)JavaScript遞迴AI
- 遞迴和尾遞迴遞迴
- ?30 秒瞭解尾遞迴和尾遞迴優化遞迴優化
- Javascript中的尾遞迴及其優化JavaScript遞迴優化
- 通過階乘的例子,練習在JavaScript, Scala和ABAP裡實現尾遞迴(Tail Recursion)JavaScript遞迴AI
- 遞迴優化:尾呼叫和Memoization遞迴優化
- 淺談尾遞迴遞迴
- 遞迴尾呼叫優化遞迴優化
- 尾遞迴以及優化遞迴優化
- 【Scala】尾遞迴優化遞迴優化
- 函數語言程式設計之尾呼叫和尾遞迴函數程式設計遞迴
- 演算法小專欄:遞迴與尾遞迴演算法遞迴
- JavaScript遞迴JavaScript遞迴
- 尾遞迴(tail recursion) 的簡單使用遞迴AI
- JavaScript中的遞迴JavaScript遞迴
- 尾遞迴實現深複製遞迴
- 直觀理解(尾)遞迴函式遞迴函式
- javascript遞迴整理JavaScript遞迴
- 瞭解 JavaScript 的遞迴JavaScript遞迴
- JavaScript 函式遞迴JavaScript函式遞迴
- 用 JavaScript 的方式理解遞迴JavaScript遞迴
- JavaScript專題之遞迴JavaScript遞迴
- JavaScript之遞迴的簡單使用JavaScript遞迴
- JS尾遞迴優化斐波拉契數列JS遞迴優化
- 週而復始,往復迴圈,遞迴、尾遞迴演算法與無限極層級結構的探究和使用(Golang1.18)遞迴演算法Golang
- JavaScript 中匿名函式的遞迴呼叫JavaScript函式遞迴
- 遞迴和遞推總結遞迴
- JavaScript演算法之遞迴JavaScript演算法遞迴
- 探索c#之尾遞迴編譯器優化C#遞迴編譯優化
- Vue3.0的遞迴監聽和非遞迴監聽Vue遞迴
- Oracle和Mysql遞迴OracleMySql遞迴
- 漢諾塔和遞迴遞迴
- JavaScript怎麼使用迴圈代替(非同步)遞迴JavaScript非同步遞迴
- 遞迴和非遞迴分別實現求n的階乘遞迴
- 斐波那契數列的遞迴和非遞迴實現遞迴
- 原:八皇后問題的遞迴和非遞迴Java實現遞迴Java
- javascript遞迴概念簡單介紹JavaScript遞迴
- javascript遞迴例項程式碼演示JavaScript遞迴