tgi是什么意思| 咳嗽白痰吃什么好得快| 燊是什么意思| 赢荡为什么传位嬴稷| 晚上睡觉脚抽筋是什么原因引起的| 胎盘低置是什么原因造成的| 哈气是什么意思| 人老珠黄是什么动物| 颈椎脑供血不足吃什么药| 什么是皮肤病| 屁是什么气体| 金牛座和什么星座不合| 榴莲的寓意是什么意思| lc是什么意思| 死心塌地什么意思| dpn是什么意思| 发情什么意思| 荨麻疹用什么药| 胳膊疼挂什么科| 肾火旺有什么症状| 心跳慢吃什么药| 23岁属什么生肖| vain是什么意思| 开飞机需要什么驾照| 各的偏旁是什么| 温水煮青蛙是什么意思| 冰箱为什么老是结冰| 子宫有积液是什么原因引起的| 吃饭流汗是什么原因| 青团是什么节日吃的| 豆乳是什么| 扒灰什么意思| 发蒙是什么意思| 5201314是什么意思| 受精卵发育成什么| 色散是什么意思| 验血脂挂什么科| 额头长痘痘是什么原因| 1998年出生属什么| alyx是什么牌子| 脆皖鱼是什么鱼| 阑尾炎有什么症状表现| 门牙旁边的牙齿叫什么| 女生排卵期是什么意思| 转述句什么意思| 良字少一点是什么字| 鱼香肉丝属于什么菜系| 篮球中锋是干什么的| 皮肤干燥是什么原因| 72年属什么生肖| 1940年中国发生了什么| 巧妙是什么意思| 骨密度t值是什么意思| 什么的肥料| 吃李子有什么好处| 什么姿势最深| 车辆购置税什么时候交| 道什么意思| moschino是什么品牌| 做梦捡到钱是什么预兆| 眼睛发炎用什么眼药水| 蹭饭吃是什么意思| 孤僻是什么意思| 拉屎有泡沫是什么原因| 看脊椎挂什么科| 世界上最小的长度单位是什么| 减肥期间晚上可以吃什么| 肛门指检是检查什么| 什么工作赚钱最快| 狼入虎口是什么意思| 3月25日什么星座| 女真人是什么民族| 止血敏又叫什么| 屈臣氏是卖什么的| 深邃是什么意思| 脂溢性皮炎用什么洗发水| 春天可以干什么| 堂哥的女儿叫什么| 冰晶是什么东西| 十二点是什么时辰| 黑眼圈看什么科| 花洒不出水什么原因| 报销凭证是什么| 卯五行属什么| 女人每天吃什么抗衰老| 肝内高回声什么意思| 为什么说啄木鸟是树的医生| 腹部b超能检查什么| 乙状结肠冗长是什么意思| 茵是什么意思| 林黛玉是个什么样的人| 怕冷吃什么药| 女人在什么时候最容易怀孕| nt什么时候做| 斯密达什么意思| 与虎谋皮是什么意思| 心率是什么意思| 草木皆兵指什么生肖| 癌症晚期吃什么食物好| 红花跟藏红花有什么区别| 癌胚抗原高是什么意思| 麻药叫什么名字| 什么叫肿瘤| maby什么意思| 咽喉炎吃什么| 长江后浪推前浪是什么意思| 肉便器是什么东西| 口若什么| 打胎用什么药| 狗和什么属相相冲| 专科什么专业就业前景好| 特种兵是什么兵种| 为什么鱼和熊掌不可兼得| 基佬是什么意思| 小孩咳嗽吃什么药效果最好| 阳虚什么症状| 什么是汛期| 出柜是什么意思| 鬼冢虎为什么很少人穿| 什么病能办低保| 黄豆酱做什么菜好吃| 左侧肚脐旁边疼是什么原因| 四两拨千斤是什么意思| 74年大溪水命缺什么| 黑裤子配什么颜色上衣| 老年人脚肿什么原因| 下巴长痘痘是什么原因| 取环后应该注意什么| 贝字旁与什么有关| 二级b超是检查什么| 老公工作劳累炖什么汤| 波推飞机什么意思| 多多关照是什么意思| 皮癣用什么药膏| 法王是什么意思| 犯太岁是什么意思| 下肢水肿是什么原因| 阿根廷讲什么语言| 胃病吃什么药最好| 印度什么时候独立的| 康乃馨的花语代表什么| 屏蔽一个人意味着什么| 查乙肝五项挂什么科| 深度睡眠是什么意思| 为什么会长疣| 茄子和什么不能一起吃| 左肺下叶纤维灶是什么意思| 甲状腺囊肿不能吃什么| 身体缺硒有什么症状| 可乐喝多了有什么危害| 法界是什么意思| 落魄是什么意思| 美的本质是什么| 发蜡和发泥有什么区别| 开口腔诊所需要什么条件| 什么一边什么一边什么| 枸橼酸西地那非片是什么药| 8月28号是什么星座| 宝宝病毒感染吃什么药效果好| 纳豆是什么| 孕吐是什么原因造成的| 河南属于什么气候| 过期蜂蜜还有什么用途| 身上痒是什么情况| 频发房性早搏是什么意思| 七月九号是什么日子| 七月十六是什么星座| 晚上睡不着吃什么药| 微信备注是什么意思| 熟地黄是什么| 开背鱼是什么鱼| 下午1点是什么时辰| 感冒喝什么水好得快| 走南闯北是什么生肖| 孕妇拉肚子可以吃什么药| 花仙子是什么意思| 梦见自己大出血是什么征兆| 考试为什么要用2b铅笔| 啤酒对身体有什么好处| 土地出让金什么意思| 怀孕肚皮痒是什么原因| 硫磺是什么东西| 狼吞虎咽的意思是什么| ct是检查什么的| 经常说梦话是什么原因| 贤淑是什么意思| 肝ca是什么意思| 不带壳的蜗牛叫什么| 双鱼座和什么座最配对| 翻盘是什么意思| 瘿瘤是什么意思| 黄芪配什么不上火| 鸡同鸭讲是什么意思| 燃气灶什么品牌好| 恩赐是什么意思| 车前草有什么作用| 脚气是什么样的| 晨勃是什么意思啊| 背痛是什么原因| 总想睡觉是什么原因| 白发多的原因是什么| 前列腺增大有什么危害| 晚上做噩梦是什么原因| 胎位roa是什么意思| 弱视和近视有什么区别| 半元音是什么意思| 现在有什么好的创业项目| 降压药什么时候吃好| 黄疸是什么病| 肌酐300多属于什么期| 美容美体是干什么的| 慢性阑尾炎吃什么药好| 8月24是什么星座| 岩茶属于什么茶| 曦字五行属什么| 前列腺炎有什么征兆| 孕妇贫血吃什么药| 痛风石是什么| 人均gdp是什么意思| 良缘是什么意思| oil什么意思| 天朝是什么意思| 高铁与动车的区别是什么| 为什么床上有蚂蚁| 脐炎用什么药| 米干是什么| leg是什么意思| 喝牛奶放屁多是什么原因| 张学良为什么不回大陆| birkin是什么意思| 门前的小树已成年是什么歌| winner是什么意思| 衢是什么意思| 梦见自己嫁人了预示着什么| 腰疼吃什么药最有效| 腰椎间盘突出看什么科| 女人喜欢什么样的男人| 赊账是什么意思| 读警校需要什么条件| 人面桃花相映红是什么意思| 花甲炒什么配菜好吃| 摆地摊卖什么最赚钱而且很受欢迎| 骨折是什么意思| 上焦中焦下焦是什么| 1953年是什么生肖| 腰椎间盘膨出是什么意思| 虎头蜂泡酒有什么功效| 农历12月是什么星座| 阴道有味道是什么原因| 年轻人血压高是什么原因引起的| 黑金刚是什么药| 什么提示你怀了女宝宝| 吃什么补白细胞快| 马蹄南去人北望是什么歌| 吃什么食物补血| 旭五行属性是什么| 雷贝拉唑钠肠溶片什么时候吃| 吃杨梅有什么好处| 摩羯座女生和什么星座男生最配| 五月十一是什么星座| 野生刺猬吃什么| 胡萝卜什么时候成熟| 办银行卡需要什么条件| 什么是血栓| 百度

《对话》 20180318 我是总师——反隐身雷达总师

百度   今年30岁的苗龙平是陇南市宕昌县城关镇鹿仁村村民。

In computer science, a tail call is a subroutine call performed as the final action of a procedure.[1] If the target of a tail is the same subroutine, the subroutine is said to be tail recursive, which is a special case of direct recursion. Tail recursion (or tail-end recursion) is particularly useful, and is often easy to optimize in implementations.

Tail calls can be implemented without adding a new stack frame to the call stack. Most of the frame of the current procedure is no longer needed, and can be replaced by the frame of the tail call, modified as appropriate (similar to overlay for processes, but for function calls). The program can then jump to the called subroutine. Producing such code instead of a standard call sequence is called tail-call elimination or tail-call optimization. Tail-call elimination allows procedure calls in tail position to be implemented as efficiently as goto statements, thus allowing efficient structured programming. In the words of Guy L. Steele, "in general, procedure calls may be usefully thought of as GOTO statements which also pass parameters, and can be uniformly coded as [machine code] JUMP instructions."[2]

Not all programming languages require tail-call elimination. However, in functional programming languages, tail-call elimination is often guaranteed by the language standard, allowing tail recursion to use a similar amount of memory as an equivalent loop. The special case of tail-recursive calls, when a function calls itself, may be more amenable to call elimination than general tail calls. When the language semantics do not explicitly support general tail calls, a compiler can often still optimize sibling calls, or tail calls to functions which take and return the same types as the caller.[3]

Description

edit

When a function is called, the computer must "remember" the place it was called from, the return address, so that it can return to that location with the result once the call is complete. Typically, this information is saved on the call stack, a list of return locations in the order that the call locations were reached. In addition, compiler allocates memory for local variables of the called function and pushes register content (if any and/or relevant) onto stack. Typically, it is done by allocating a stack frame including saved registers, space allocated for non-register local variables, return address and call parameters (unless they are passed in registers). For tail calls, there is no need to remember the caller or preserve content of registers – instead, tail-call elimination avoids allocation of new stack frames and makes only the minimum necessary changes to the existing stack frame before passing it on,[4] and the tail-called function will return directly to the original caller. This, however, leads to complete loss of the caller's stack frame, which is sometimes considered as a hindrance in debugging. The tail call doesn't have to appear lexically after all other statements in the source code; it is only important that the calling function return immediately after the tail call, returning the tail call's result if any, since the calling function is bypassed when the optimization is performed.

For non-recursive function calls, this is usually an optimization that saves only a little time and space, since there are not that many different functions available to call. When dealing with recursive or mutually recursive functions where recursion happens through tail calls, however, the stack space and the number of returns saved can grow to be very significant, since a function can call itself, directly or indirectly, creating a new call stack frame each time. Tail-call elimination often reduces asymptotic stack space requirements from linear, or O(n), to constant, or O(1). Tail-call elimination is thus required by the standard definitions of some programming languages, such as Scheme,[5][6] and languages in the ML family among others. The Scheme language definition formalizes the intuitive notion of tail position exactly, by specifying which syntactic forms allow having results in tail context.[7] Implementations allowing an unlimited number of tail calls to be active at the same moment, thanks to tail-call elimination, can also be called 'properly tail recursive'.[5]

Besides space and execution efficiency, tail-call elimination is important in the functional programming idiom known as continuation-passing style (CPS), which would otherwise quickly run out of stack space.

Syntactic form

edit

A tail call can be located just before the syntactical end of a function:

function foo(data) {
    a(data);
    return b(data);
}

Here, both a(data) and b(data) are calls, but b is the last thing the procedure executes before returning and is thus in tail position. However, not all tail calls are necessarily located at the syntactical end of a subroutine:

function bar(data) {
    if (a(data)) {
        return b(data);
    }
    return c(data);
}

Here, both calls to b and c are in tail position. This is because each of them lies in the end of if-branch respectively, even though the first one is not syntactically at the end of bar's body.

In this code:

function foo1(data) {
    return a(data) + 1;
}
function foo2(data) {
    var ret = a(data);
    return ret;
}
function foo3(data) {
    var ret = a(data);
    return (ret == 0) ? 1 : ret;
}

the call to a(data) is in tail position in foo2, but it is not in tail position either in foo1 or in foo3, because control must return to the caller to allow it to inspect or modify the return value before returning it.

Example programs

edit

The following program is an example in Scheme:[8]

;; factorial?: number -> number
;; to calculate the product of all positive
;; integers less than or equal to n.
(define (factorial n)
 (if (= n 0)
    1
    (* n (factorial (- n 1)))))

This is not written in a tail-recursive style, because the multiplication function ("*") is in the tail position. This can be compared to:

;; factorial?: number -> number
;; to calculate the product of all positive
;; integers less than or equal to n.
(define (factorial n)
  (fact-iter 1 n))
(define (fact-iter product n)
  (if (= n 0)
      product
      (fact-iter (* product n)
                 (- n 1))))

This program assumes applicative-order evaluation. The inner procedure fact-iter calls itself last in the control flow. This allows an interpreter or compiler to reorganize the execution which would ordinarily look like this:[8]

  call factorial (4)
   call fact-iter (1 4)
    call fact-iter (4 3)
     call fact-iter (12 2)
      call fact-iter (24 1)
      return 24
     return 24
    return 24
   return 24
  return 24

into the more efficient variant, in terms of both space and time:

  call factorial (4)
   call fact-iter (1 4)
   replace arguments with (4 3)
   replace arguments with (12 2)
   replace arguments with (24 1)
   return 24
  return 24

This reorganization saves space because no state except for the calling function's address needs to be saved, either on the stack or on the heap, and the call stack frame for fact-iter is reused for the intermediate results storage. This also means that the programmer need not worry about running out of stack or heap space for extremely deep recursions. In typical implementations, the tail-recursive variant will be substantially faster than the other variant, but only by a constant factor.

Some programmers working in functional languages will rewrite recursive code to be tail recursive so they can take advantage of this feature. This often requires addition of an "accumulator" argument (product in the above example) to the function.

Tail recursion modulo cons

edit

Tail recursion modulo cons is a generalization of tail-recursion optimization introduced by David H. D. Warren[9] in the context of compilation of Prolog, seen as an explicitly set once language. It was described (though not named) by Daniel P. Friedman and David S. Wise in 1974[10] as a LISP compilation technique. As the name suggests, it applies when the only operation left to perform after a recursive call is to prepend a known value in front of the list returned from it (or to perform a constant number of simple data-constructing operations, in general). This call would thus be a tail call save for ("modulo") the said cons operation. But prefixing a value at the start of a list on exit from a recursive call is the same as appending this value at the end of the growing list on entry into the recursive call, thus building the list as a side effect, as if in an implicit accumulator parameter. The following Prolog fragment illustrates the concept:

Example code

edit
% Prolog, tail recursive modulo cons:
partition([], _, [], []).
partition([X|Xs], Pivot, [X|Rest], Bigs) :-
  X @< Pivot, !,
  partition(Xs, Pivot, Rest, Bigs).
partition([X|Xs], Pivot, Smalls, [X|Rest]) :-
  partition(Xs, Pivot, Smalls, Rest).
-- In Haskell, guarded recursion:
partition [] _ = ([],[])
partition (x:xs) p 
        | x < p     = (x:a,b)
        | otherwise = (a,x:b)
   where
      (a,b) = partition xs p
% Prolog, with explicit unifications:
%     non-tail recursive translation:
partition([], _, [], []).
partition(L, Pivot, Smalls, Bigs) :- L=[X|Xs],
 (  X @< Pivot
 -> partition(Xs,Pivot,Rest,Bigs), Smalls=[X|Rest]
 ;  partition(Xs,Pivot,Smalls,Rest), Bigs=[X|Rest]
 ).
% Prolog, with explicit unifications:
%     tail-recursive translation:
partition([], _, [], []).
partition(L, Pivot, Smalls, Bigs) :- L=[X|Xs],
 (  X @< Pivot
 -> Smalls=[X|Rest], partition(Xs,Pivot,Rest,Bigs)
 ;  Bigs=[X|Rest], partition(Xs,Pivot,Smalls,Rest)
 ).

Thus in tail-recursive translation such a call is transformed into first creating a new list node and setting its first field, and then making the tail call with the pointer to the node's rest field as argument, to be filled recursively. The same effect is achieved when the recursion is guarded under a lazily evaluated data constructor, which is automatically achieved in lazy programming languages like Haskell.

C example

edit

The following fragment defines a recursive function in C that duplicates a linked list (with some equivalent Scheme and Prolog code as comments, for comparison):

typedef struct list {
    void *value;
    struct list *next;
} list;

list *duplicate(const list *ls)
{
    list *head = NULL;

    if (ls != NULL) {
        list *p = duplicate(ls->next);
        head = malloc(sizeof *head);
        head->value = ls->value;
        head->next = p;
    }
    return head;
}
;; in Scheme,
(define (duplicate ls)
  (if (not (null? ls))
    (cons (car ls)
          (duplicate (cdr ls)))
    '()))
%% in Prolog,
duplicate([X|Xs],R):-
  duplicate(Xs,Ys),
  R=[X|Ys].
duplicate([],[]).

In this form the function is not tail recursive, because control returns to the caller after the recursive call duplicates the rest of the input list. Even if it were to allocate the head node before duplicating the rest, it would still need to plug in the result of the recursive call into the next field after the call.[a] So the function is almost tail recursive. Warren's method pushes the responsibility of filling the next field into the recursive call itself, which thus becomes tail call.[b] Using sentinel head node to simplify the code,

typedef struct list {
    void *value;
    struct list *next;
} list;
void duplicate_aux(const list *ls, list *end);

list *duplicate(const list *ls)
{  
    list head;

    duplicate_aux(ls, &head);
    return head.next;
}

void duplicate_aux(const list *ls, list *end)
{
    if (ls != NULL) {
        end->next = malloc(sizeof *end);
        end->next->value = ls->value;
        duplicate_aux(ls->next, end->next);
    } else {
        end->next = NULL;
    }
}
;; in Scheme,
(define (duplicate ls)
  (let ((head (list 1)))
    (let dup ((ls  ls)
              (end head))
      (cond
        ((not (null? ls))
         (set-cdr! end (list (car ls)))
         (dup (cdr ls) (cdr end)))))
    (cdr head)))
%% in Prolog,
duplicate([X|Xs],R):-
   R=[X|Ys],
   duplicate(Xs,Ys).
duplicate([],[]).

The callee now appends to the end of the growing list, rather than have the caller prepend to the beginning of the returned list. The work is now done on the way forward from the list's start, before the recursive call which then proceeds further, instead of backward from the list's end, after the recursive call has returned its result. It is thus similar to the accumulating parameter technique, turning a recursive computation into an iterative one.

Characteristically for this technique, a parent frame is created on the execution call stack, which the tail-recursive callee can reuse as its own call frame if the tail-call optimization is present.

The tail-recursive implementation can now be converted into an explicitly iterative implementation, as an accumulating loop:

typedef struct list {
    void *value;
    struct list *next;
} list;

list *duplicate(const list *ls)
{
    list head, *end;
    end = &head;
    while (ls != NULL)
    {
        end->next        = malloc(sizeof *end);
        end->next->value = ls->value;
        ls = ls->next;
        end = end->next;
    }
    end->next = NULL;
    return head.next;
}
 ;; in Scheme,
 (define (duplicate ls)
   (let ((head (list 1)))
     (do ((end head (cdr end))
          (ls  ls   (cdr ls )))
         ((null? ls) (cdr head))
       (set-cdr! end (list (car ls))))))
%% in Prolog,
%% N/A

History

edit

In a paper delivered to the ACM conference in Seattle in 1977, Guy L. Steele summarized the debate over the GOTO and structured programming, and observed that procedure calls in the tail position of a procedure can be best treated as a direct transfer of control to the called procedure, typically eliminating unnecessary stack manipulation operations.[2] Since such "tail calls" are very common in Lisp, a language where procedure calls are ubiquitous, this form of optimization considerably reduces the cost of a procedure call compared to other implementations. Steele argued that poorly-implemented procedure calls had led to an artificial perception that the GOTO was cheap compared to the procedure call. Steele further argued that "in general procedure calls may be usefully thought of as GOTO statements which also pass parameters, and can be uniformly coded as [machine code] JUMP instructions", with the machine code stack manipulation instructions "considered an optimization (rather than vice versa!)".[2] Steele cited evidence that well-optimized numerical algorithms in Lisp could execute faster than code produced by then-available commercial Fortran compilers because the cost of a procedure call in Lisp was much lower. In Scheme, a Lisp dialect developed by Steele with Gerald Jay Sussman, tail-call elimination is guaranteed to be implemented in any interpreter.[11]

Implementation methods

edit

Tail recursion is important to some high-level languages, especially functional and logic languages and members of the Lisp family. In these languages, tail recursion is the most commonly used way (and sometimes the only way available) of implementing iteration. The language specification of Scheme requires that tail calls are to be optimized so as not to grow the stack. Tail calls can be made explicitly in Perl, with a variant of the "goto" statement that takes a function name: goto &NAME;[12]

However, for language implementations which store function arguments and local variables on a call stack (which is the default implementation for many languages, at least on systems with a hardware stack, such as the x86), implementing generalized tail-call optimization (including mutual tail recursion) presents an issue: if the size of the callee's activation record is different from that of the caller, then additional cleanup or resizing of the stack frame may be required. For these cases, optimizing tail recursion remains trivial, but general tail-call optimization may be harder to implement efficiently.

For example, in the Java virtual machine (JVM), tail-recursive calls can be eliminated (as this reuses the existing call stack), but general tail calls cannot be (as this changes the call stack).[13][14] As a result, functional languages such as Scala that target the JVM can efficiently implement direct tail recursion, but not mutual tail recursion.

The GCC, LLVM/Clang, and Intel compiler suites perform tail-call optimization for C and other languages at higher optimization levels or when the -foptimize-sibling-calls option is passed.[15][16][17] Though the given language syntax may not explicitly support it, the compiler can make this optimization whenever it can determine that the return types for the caller and callee are equivalent, and that the argument types passed to both function are either the same, or require the same amount of total storage space on the call stack.[18]

Various implementation methods are available.

In assembly

edit

Tail calls are often optimized by interpreters and compilers of functional programming and logic programming languages to more efficient forms of iteration. For example, Scheme programmers commonly express while loops as calls to procedures in tail position and rely on the Scheme compiler or interpreter to substitute the tail calls with more efficient jump instructions.[19]

For compilers generating assembly directly, tail-call elimination is easy: it suffices to replace a call opcode with a jump one, after fixing parameters on the stack. From a compiler's perspective, the first example above is initially translated into pseudo-assembly language (in fact, this is valid x86 assembly):

 foo:
  call B
  call A
  ret

Tail-call elimination replaces the last two lines with a single jump instruction:

 foo:
  call B
  jmp  A

After subroutine A completes, it will then return directly to the return address of foo, omitting the unnecessary ret statement.

Typically, the subroutines being called need to be supplied with parameters. The generated code thus needs to make sure that the call frame for A is properly set up before jumping to the tail-called subroutine. For instance, on platforms where the call stack does not just contain the return address, but also the parameters for the subroutine, the compiler may need to emit instructions to adjust the call stack. On such a platform, for the code:

function foo(data1, data2)
   B(data1)
   return A(data2)

(where data1 and data2 are parameters) a compiler might translate that as:[c]

 foo:
   mov  reg,[sp+data1] ; fetch data1 from stack (sp) parameter into a scratch register.
   push reg            ; put data1 on stack where B expects it
   call B              ; B uses data1
   pop                 ; remove data1 from stack
   mov  reg,[sp+data2] ; fetch data2 from stack (sp) parameter into a scratch register.
   push reg            ; put data2 on stack where A expects it
   call A              ; A uses data2
   pop                 ; remove data2 from stack.
   ret

A tail-call optimizer could then change the code to:

 foo:
   mov  reg,[sp+data1] ; fetch data1 from stack (sp) parameter into a scratch register.
   push reg            ; put data1 on stack where B expects it
   call B              ; B uses data1
   pop                 ; remove data1 from stack
   mov  reg,[sp+data2] ; fetch data2 from stack (sp) parameter into a scratch register.
   mov  [sp+data1],reg ; put data2 where A expects it
   jmp  A              ; A uses data2 and returns immediately to caller.

This code is more efficient both in terms of execution speed and use of stack space.

Through trampolining

edit

Since many Scheme compilers use C as an intermediate target code, the tail recursion must be encoded in C without growing the stack, even if the C compiler does not optimize tail calls. Many implementations achieve this by using a device known as a trampoline, a piece of code that repeatedly calls functions. All functions are entered via the trampoline. When a function has to tail-call another, instead of calling it directly and then returning the result, it returns the address of the function to be called and the call parameters back to the trampoline (from which it was called itself), and the trampoline takes care of calling this function next with the specified parameters. This ensures that the C stack does not grow and iteration can continue indefinitely.

It is possible to implement trampolines using higher-order functions in languages that support them, such as Groovy, Visual Basic .NET and C#.[20]

Using a trampoline for all function calls is rather more expensive than the normal C function call, so at least one Scheme compiler, Chicken, uses a technique first described by Henry Baker from an unpublished suggestion by Andrew Appel,[21] in which normal C?calls are used but the stack size is checked before every call. When the stack reaches its maximum permitted size, objects on the stack are garbage-collected using the Cheney algorithm by moving all live data into a separate heap. Following this, the stack is unwound ("popped") and the program resumes from the state saved just before the garbage collection. Baker says "Appel's method avoids making a large number of small trampoline bounces by occasionally jumping off the Empire State Building."[21] The garbage collection ensures that mutual tail recursion can continue indefinitely. However, this approach requires that no C function call ever returns, since there is no guarantee that its caller's stack frame still exists; therefore, it involves a much more dramatic internal rewriting of the program code: continuation-passing style.

Relation to the while statement

edit

Tail recursion can be related to the while statement, an explicit iteration, for instance by transforming

procedure foo(x)
    if p(x)
        return bar(x)
    else
        return foo(baz(x))

into

procedure foo(x)
    while true
        if p(x)
            return bar(x)
        else
            x ← baz(x)

where x may be a tuple involving more than one variable: if so, care must be taken in implementing the assignment statement x ← baz(x) so that dependencies are respected. One may need to introduce auxiliary variables or use a swap construct.

More generally,

procedure foo(x)
    if p(x)
        return bar(x)
    else if q(x)
        return baz(x)
    ...
    else if r(x)
        return foo(qux(x))
    ...
    else
        return foo(quux(x))

can be transformed into

procedure foo(x)
    while true
        if p(x)
            return bar(x)
        else if q(x)
            return baz(x)
        ...
        else if r(x)
            x ← qux(x)
        ...
        else
            x ← quux(x)

For instance, this Julia program gives a non-tail recursive definition fact of the factorial:

function factorial(n)
    if n == 0
        return 1
    else
        return n * factorial(n - 1)
    end
end

Indeed, n * factorial(n - 1) wraps the call to factorial. But it can be transformed into a tail-recursive definition by adding an argument a called an accumulator.[8]

This Julia program gives a tail-recursive definition fact_iter of the factorial:

function factorial(n::Integer, a::Integer)
    if n == 0:
        return a
    else
        return factorial(n - 1, n * a)
    end
end

function factorial(n::Integer)
    return factorial(n, 1)
end

This Julia program gives an iterative definition fact_iter of the factorial:

function fact_iter(n::Integer, a::Integer)
    while n > 0
        a = n * a
        n = n - 1
    end
    return a
end

function factorial(n::Integer)
    return fact_iter(n, one(n))
end

Language support

edit
  • C++ – C and C++ both do tail-call optimization.[22]
  • Clojure – Clojure has recur special form.[23]
  • Common Lisp – Some implementations perform tail-call optimization during compilation if optimizing for speed
  • Elixir – Elixir implements tail-call optimization, [24]as do all languages currently targeting the BEAM VM.
  • Elm – Yes[25]
  • Erlang – Yes
  • F# – F# implements TCO by default where possible [26]
  • Go – No support[27]
  • Haskell – Yes[28]
  • JavaScriptECMAScript 6.0 compliant engines should have tail calls[29] which is now implemented on Safari/WebKit[30] but rejected by V8 and SpiderMonkey
  • Kotlin – Has tailrec modifier for functions[31]
  • Lua – Tail recursion is required by the language definition[32]
  • Objective-C – Compiler optimizes tail calls when -O1 (or higher) option specified, but it is easily disturbed by calls added by Automatic Reference Counting (ARC).
  • OCaml – Yes
  • Perl – Explicit with a variant of the "goto" statement that takes a function name: goto &NAME;[33]
  • PureScript – Yes
  • Python – Stock Python implementations do not perform tail-call optimization, though a third-party module is available to do this.[34] Language inventor Guido van Rossum contended that stack traces are altered by tail-call elimination making debugging harder, and preferred that programmers use explicit iteration instead.[35] In Python 3.14, a new interpreter was introduced that uses tail-call based dispatch of Python opcodes.[36] This resulted in overall improved performance when compared to Python 3.13.[37][38]
  • R – Yes, tailcall() function introduced in R.4.4.0[39]
  • Racket – Yes[40]
  • Ruby – Yes, but disabled by default [41]
  • Rust – tail-call optimization may be done in limited circumstances, but is not guaranteed[42]
  • Scala – Tail-recursive functions are automatically optimized by the compiler. Such functions can also optionally be marked with a @tailrec annotation, which makes it a compilation error if the function is not tail recursive[43]
  • Scheme – Required by the language definition[44][45]
  • Swift – In some cases (as of 2014).[46]
  • Tcl – Since Tcl 8.6, Tcl has a tailcall command[47]
  • Zig – Yes[48]

See also

edit

Notes

edit
  1. ^ Like this:
    if (ls != NULL) {
      head = malloc( sizeof *head);
      head->value = ls->value;
      head->next = duplicate( ls->next);
    }
    
  2. ^ Like this:
    if (ls != NULL) {
      head = malloc( sizeof *head);
      head->value = ls->value;
      duplicate( ls->next, &(head->next));
    }
    
  3. ^ The call instruction first pushes the current code location onto the stack and then performs an unconditional jump to the code location indicated by the label. The ret instruction first pops a code location off the stack, then performs an unconditional jump to the retrieved code location.

References

edit
  1. ^ Steven Muchnick; Muchnick and Associates (15 August 1997). Advanced Compiler Design Implementation. Morgan Kaufmann. ISBN?978-1-55860-320-2.
  2. ^ a b c Steele, Guy Lewis (1977). "Debunking the "expensive procedure call" myth or, procedure call implementations considered harmful or, LAMBDA: The Ultimate GOTO". Proceedings of the 1977 annual conference on - ACM '77. pp.?153–162. doi:10.1145/800179.810196. hdl:1721.1/5753. ISBN?978-1-4503-2308-6. S2CID?9807843.
  3. ^ "The LLVM Target-Independent Code Generator — LLVM 7 documentation". llvm.org.
  4. ^ "recursion - Stack memory usage for tail calls - Theoretical Computer Science". Cstheory.stackexchange.com. 2025-08-14. Retrieved 2025-08-14.
  5. ^ a b "Revised^6 Report on the Algorithmic Language Scheme". R6rs.org. Retrieved 2025-08-14.
  6. ^ "Revised^6 Report on the Algorithmic Language Scheme - Rationale". R6rs.org. Retrieved 2025-08-14.
  7. ^ "Revised^6 Report on the Algorithmic Language Scheme". R6rs.org. Retrieved 2025-08-14.
  8. ^ a b c Sussman, G. J.; Abelson, Hal (1984). Structure and Interpretation of Computer Programs. Cambridge, MA: MIT Press. ISBN?0-262-01077-1.
  9. ^ D. H. D. Warren, DAI Research Report 141, University of Edinburgh, 1980.
  10. ^ Daniel P. Friedman and David S. Wise, Technical Report TR19: Unwinding Structured Recursions into Iterations, Indiana University, Dec. 1974. PDF available here (webarchived copy here).
  11. ^ R5RS Sec. 3.5, Richard Kelsey; William Clinger; Jonathan Rees; et?al. (August 1998). "Revised5 Report on the Algorithmic Language Scheme". Higher-Order and Symbolic Computation. 11 (1): 7–105. doi:10.1023/A:1010051815785. S2CID?14069423.
  12. ^ Contact details. "goto". perldoc.perl.org. Retrieved 2025-08-14.
  13. ^ "What is difference between tail calls and tail recursion?", Stack Overflow
  14. ^ "What limitations does the JVM impose on tail-call optimization", Programmers Stack Exchange
  15. ^ Lattner, Chris. "LLVM Language Reference Manual, section: The LLVM Target-Independent Code Generator, sub: Tail Call Optimization". The LLVM Compiler Infrastructure. The LLVM Project. Retrieved 24 June 2018.
  16. ^ "Using the GNU Compiler Collection (GCC): Optimize Options". gcc.gnu.org.
  17. ^ "foptimize-sibling-calls". software.intel.com.
  18. ^ "Tackling C++ Tail Calls".
  19. ^ Probst, Mark (20 July 2000). "proper tail recursion for gcc". GCC Project. Retrieved 10 March 2015.
  20. ^ Samuel Jack, Bouncing on your tail. Functional Fun. April 9, 2008.
  21. ^ a b Henry Baker, "CONS Should Not CONS Its Arguments, Part II: Cheney on the M.T.A." Archived 2025-08-14 at the Wayback Machine
  22. ^ "Which, if any, C++ compilers do tail-recursion optimization?". stackoverflow.
  23. ^ "(recur expr*)". clojure.org.
  24. ^ "Recursion". elixir-lang.github.com.
  25. ^ Czaplicki, Evan. "Functional Programming in Elm: Tail-Call Elimination".
  26. ^ "Tail Calls in F#". msdn. Microsoft. 8 July 2011.
  27. ^ "proposal: Go 2: add become statement to support tail calls". github.com.
  28. ^ "Tail recursion - HaskellWiki". wiki.haskell.org. Retrieved 2025-08-14.
  29. ^ Beres-Deak, Adam. "Worth watching: Douglas Crockford speaking about the good new parts of JavaScript in 2014". bdadam.com.
  30. ^ "ECMAScript 6 in WebKit". 13 October 2015.
  31. ^ "Functions: infix, vararg, tailrec - Kotlin Programming Language". Kotlin.
  32. ^ "Lua 5.3 Reference Manual". www.lua.org.
  33. ^ "goto - perldoc.perl.org". perldoc.perl.org.
  34. ^ "baruchel/tco". GitHub. 29 March 2022.
  35. ^ Rossum, Guido Van (22 April 2009). "Neopythonic: Tail Recursion Elimination".
  36. ^ "Tail-calling interpreter". GitHub. 2025-08-14. Retrieved 2025-08-14.
  37. ^ "What's new in Python 3.14". Python documentation. Retrieved 2025-08-14.
  38. ^ "A new tail-calling interpreter for significantly better interpreter performance". GitHub. 2025-08-14. Retrieved 2025-08-14.
  39. ^ "What's new in R 4.4.0?". www.jumpingrivers.com. 2025-08-14. Retrieved 2025-08-14.
  40. ^ "The Racket Reference". docs.racket-lang.org.
  41. ^ "Ruby Tail Call Optimisation".
  42. ^ "Rust FAQ". prev.rust-lang.org.
  43. ^ "Scala Standard Library 2.13.0 - scala.annotation.tailrec". www.scala-lang.org. Retrieved 2025-08-14.
  44. ^ "Revised^5 Report on the Algorithmic Language Scheme". www.schemers.org.
  45. ^ "Revised^6 Report on the Algorithmic Language Scheme". www.r6rs.org.
  46. ^ "Does Swift implement tail call optimization?". 2014. Retrieved 13 March 2024.
  47. ^ "tailcall manual page - Tcl Built-In Commands". www.tcl.tk.
  48. ^ "Documentation - the Zig Programming Language".
宝宝尿少是什么原因 永无止境是什么意思 指甲紫色是什么原因 什么解辣 吕洞宾代表什么生肖
铁蛋白是查什么的 君子兰不开花是什么原因 奶茶里面的珍珠是什么做的 为什么山东人个子高 什么洗发水好
灵魂契合是什么意思 鹤膝风是什么病 8月11是什么星座 什么是蜂胶 男性尿道刺痛吃什么药
什么叫物质女人 文化大革命什么时候结束 吃什么对血栓好 幼儿园什么时候放暑假 二十岁是什么之年
取环后月经量少是什么原因hcv8jop4ns1r.cn 胎儿偏小吃什么补得快hcv8jop5ns6r.cn 陈皮是什么皮做的hcv8jop4ns2r.cn 2000属什么生肖hcv7jop9ns1r.cn 门牙下面的牙叫什么wzqsfys.com
吃无花果干有什么好处hcv7jop9ns3r.cn 什么叫肝腹水hcv8jop2ns2r.cn 欲言又止下一句是什么hcv9jop2ns0r.cn 苏州为什么不建机场hcv7jop9ns3r.cn 喝苏打水有什么好处hcv8jop0ns9r.cn
毛囊炎是什么样子hcv9jop7ns3r.cn 高密度脂蛋白胆固醇高是什么意思hcv8jop5ns7r.cn 甲状腺彩超能查出什么hcv7jop6ns8r.cn 举贤不避亲什么意思hcv8jop1ns5r.cn 猪头三是什么意思hcv8jop9ns4r.cn
正月开什么花hcv9jop5ns2r.cn t是什么意思hcv8jop0ns3r.cn 心脏疼挂什么科hcv9jop8ns0r.cn 光屏是什么hcv8jop7ns4r.cn 菊花茶为什么会变绿色hcv9jop3ns8r.cn
百度