通過階乘的例子,練習在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
- JavaScript和ABAP的尾遞迴JavaScript遞迴
- 遞迴和非遞迴分別實現求n的階乘遞迴
- c#遞迴實現 階乘C#遞迴
- 遞迴和尾遞迴遞迴
- 遞迴的列印和階乘運用遞迴
- 尾呼叫和尾遞迴遞迴
- 尾遞迴實現深複製遞迴
- ?30 秒瞭解尾遞迴和尾遞迴優化遞迴優化
- Javascript中的尾遞迴及其優化JavaScript遞迴優化
- 通過實際的例子,介紹 SAP ABAP 裡的 Repository Information System 的使用技巧ORM
- ABAP mesh表示式, JavaScript和Scala的 expressionJavaScriptExpress
- 用遞迴方法求10的階乘遞迴
- 遞迴優化:尾呼叫和Memoization遞迴優化
- 通過一個例子學習Kubernetes裡的PersistentVolumeClaim的用法AI
- 淺談尾遞迴遞迴
- 通過一個實際的例子學習 combineLatest
- ABAP 真的會過時嗎?聊聊 ABAP 的過去,現在和未來
- JavaScript:利用遞迴實現物件深拷貝JavaScript遞迴物件
- 遞迴尾呼叫優化遞迴優化
- 尾遞迴以及優化遞迴優化
- 函數語言程式設計之尾呼叫和尾遞迴函數程式設計遞迴
- Android遍歷所有控制元件的遞迴和非遞迴實現Android控制元件遞迴
- java常見遞迴練習題Java遞迴
- 演算法小專欄:遞迴與尾遞迴演算法遞迴
- 如何通過 JavaScript 實現機器學習和神經學網路?JavaScript機器學習
- 二分法的簡單實現——-遞迴和非遞迴遞迴
- JavaScript遞迴JavaScript遞迴
- JavaScript中的遞迴JavaScript遞迴
- 遞迴與分治演算法練習遞迴演算法
- 棧實現遞迴遞迴
- lambda實現遞迴遞迴
- [程式設計題]從尾到頭列印連結串列 牛客網練習 java遞迴程式設計Java遞迴
- 迴圈高階綜合練習
- 遞迴中的遞推與迴歸以及返回值和函式儲存的問題(以階乘問題進行討論)遞迴函式
- 瞭解 JavaScript 的遞迴JavaScript遞迴
- python-動態規劃的遞迴、非遞迴實現Python動態規劃遞迴
- 透過現實生活中一個例子來理解 JavaScript PromiseJavaScriptPromise