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遞迴優化
- 淺談尾遞迴遞迴
- 遞迴尾呼叫優化遞迴優化
- 尾遞迴以及優化遞迴優化
- 函數語言程式設計之尾呼叫和尾遞迴函數程式設計遞迴
- 演算法小專欄:遞迴與尾遞迴演算法遞迴
- JavaScript遞迴JavaScript遞迴
- JavaScript中的遞迴JavaScript遞迴
- 尾遞迴實現深複製遞迴
- 瞭解 JavaScript 的遞迴JavaScript遞迴
- 用 JavaScript 的方式理解遞迴JavaScript遞迴
- 什麼是遞迴?遞迴和迴圈的異同遞迴
- JavaScript之遞迴的簡單使用JavaScript遞迴
- 週而復始,往復迴圈,遞迴、尾遞迴演算法與無限極層級結構的探究和使用(Golang1.18)遞迴演算法Golang
- JavaScript演算法之遞迴JavaScript演算法遞迴
- 遞迴和遞推總結遞迴
- JS尾遞迴優化斐波拉契數列JS遞迴優化
- Vue3.0的遞迴監聽和非遞迴監聽Vue遞迴
- JavaScript的值傳遞和引用傳遞JavaScript
- Oracle和Mysql遞迴OracleMySql遞迴
- 遞迴和非遞迴分別實現求n的階乘遞迴
- ABAP mesh表示式, JavaScript和Scala的 expressionJavaScriptExpress
- JavaScript 是如何工作的:JavaScript 的共享傳遞和按值傳遞JavaScript
- Android遍歷所有控制元件的遞迴和非遞迴實現Android控制元件遞迴
- 目標和——遞迴的實踐遞迴
- 尾遞迴 - 杜絕記憶體洩漏溢位爆棧遞迴記憶體
- 漢諾塔和遞迴遞迴
- 對遞迴和迭代的效率的思考和分析遞迴
- 二分法的簡單實現——-遞迴和非遞迴遞迴
- 舉例說明你對尾遞迴的理解,有哪些應用場景遞迴
- 遞迴的應用場景和呼叫機制、遞迴需要遵守的重要規則遞迴
- JavaScript:利用遞迴實現物件深拷貝JavaScript遞迴物件