Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize tail delivery in JavaScript, Scala and ABAP

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Tail Recursion is implemented in JavaScript, Scala and ABAP. In order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

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 note 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. This is the answer to the question about the implementation of tail recursion (Tail Recursion) in JavaScript, Scala and ABAP. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report