通過階乘的例子,練習在JavaScript, Scala和ABAP裡實現尾遞迴(Tail Recursion)
Before we start to research tail recursion, let’s first have a look at the normal recursion.
A simple factorial implementation by recursion:
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:
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:
And here comes tail recursion version:
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2703498/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- JavaScript, ABAP和Scala裡的尾遞迴(Tail Recursion)JavaScript遞迴AI
- 尾遞迴(tail recursion) 的簡單使用遞迴AI
- JavaScript和ABAP的尾遞迴JavaScript遞迴
- 遞迴和非遞迴分別實現求n的階乘遞迴
- c#遞迴實現 階乘C#遞迴
- 【Scala】尾遞迴優化遞迴優化
- 遞迴的列印和階乘運用遞迴
- 遞迴和尾遞迴遞迴
- 尾遞迴實現深複製遞迴
- ?30 秒瞭解尾遞迴和尾遞迴優化遞迴優化
- Javascript中的尾遞迴及其優化JavaScript遞迴優化
- 通過實際的例子,介紹 SAP ABAP 裡的 Repository Information System 的使用技巧ORM
- ABAP mesh表示式, JavaScript和Scala的 expressionJavaScriptExpress
- 轉:在ABAP中實現進度條的例子
- C語言用遞迴方法求解階乘C語言遞迴
- 遞迴優化:尾呼叫和Memoization遞迴優化
- Clojure編寫一個階乘程式 使用遞迴遞迴
- 淺談尾遞迴遞迴
- 斐波那契數列的遞迴和非遞迴實現遞迴
- 原:八皇后問題的遞迴和非遞迴Java實現遞迴Java
- 爬臺階問題(遞迴和動態規劃實現)遞迴動態規劃
- 遞迴尾呼叫優化遞迴優化
- 尾遞迴以及優化遞迴優化
- java常見遞迴練習題Java遞迴
- 二叉樹遞迴練習二叉樹遞迴
- JavaScript:利用遞迴實現物件深拷貝JavaScript遞迴物件
- 函數語言程式設計之尾呼叫和尾遞迴函數程式設計遞迴
- 逐步學習什麼是遞迴?通過使用場景來深入認識遞迴。遞迴
- 迴圈高階綜合練習
- 演算法小專欄:遞迴與尾遞迴演算法遞迴
- strcmp的遞迴實現遞迴
- 通過一個例子學習Kubernetes裡的PersistentVolumeClaim的用法AI
- 遞迴與分治演算法練習遞迴演算法
- 二分法的簡單實現——-遞迴和非遞迴遞迴
- Android遍歷所有控制元件的遞迴和非遞迴實現Android控制元件遞迴
- 在ABAP裡實現條件斷點的三種方式斷點
- JavaScript遞迴JavaScript遞迴
- 棧實現遞迴遞迴