tail-call-optimization

What is tail-recursion elimination?

吃可爱长大的小学妹 提交于 2019-11-27 10:43:37
问题 Steve Yegge mentioned it in a blog post and I have no idea what it means, could someone fill me in? Is it the same thing as tail call optimization? 回答1: Tail call elimination is an optimization that saves stack space. It replaces a function call with a goto . Tail recursion elimination is the same thing, but with the added constraint that the function is calling itself. Basically, if the very last thing a function A does is return A(params...) then you can eliminate the allocation of a stack

Tail Call Optimization in Go

感情迁移 提交于 2019-11-27 02:30:39
问题 Does the Go programming language, as of now, optimize tail calls ? If not, does it at least optimize tail-recursive calls of a function to itself? 回答1: Everything you can find over the Internet, that "Go supports tailable recursions in some cases", and that was told in mailing list: It is already there in 6g/8g for certain cases, and in gccgo somewhat more generally. We do not currently plan to change the language to require that compilers implement tail call optimization in all cases. If you

How do I know if a function is tail recursive in F#

别来无恙 提交于 2019-11-27 01:53:18
问题 I wrote the follwing function: let str2lst str = let rec f s acc = match s with | "" -> acc | _ -> f (s.Substring 1) (s.[0]::acc) f str [] How can I know if the F# compiler turned it into a loop? Is there a way to find out without using Reflector (I have no experience with Reflector and I Don't know C#)? Edit: Also, is it possible to write a tail recursive function without using an inner function, or is it necessary for the loop to reside in? Also, Is there a function in F# std lib to run a

Why won't the Scala compiler apply tail call optimization unless a method is final?

依然范特西╮ 提交于 2019-11-27 00:58:41
Why won't the Scala compiler apply tail call optimization unless a method is final? For example, this: class C { @tailrec def fact(n: Int, result: Int): Int = if(n == 0) result else fact(n - 1, n * result) } results in error: could not optimize @tailrec annotated method: it is neither private nor final so can be overridden What exactly would go wrong if the compiler applied TCO in a case such as this? Consider the following interaction with the REPL. First we define a class with a factorial method: scala> class C { def fact(n: Int, result: Int): Int = if(n == 0) result else fact(n - 1, n *

Tail call optimization in Mathematica?

妖精的绣舞 提交于 2019-11-27 00:10:12
问题 While formulating an answer to another SO question, I came across some strange behaviour regarding tail recursion in Mathematica. The Mathematica documentation hints that tail call optimization might be performed. But my own experiments give conflicting results. Contrast, for example, the following two expressions. The first crashes the 7.0.1 kernel, presumably due to stack exhaustion: (* warning: crashes the kernel! *) Module[{f, n = 0}, f[x_] := (n += 1; f[x + 1]); TimeConstrained[Block[{

Node.js tail-call optimization: possible or not?

不问归期 提交于 2019-11-26 17:38:19
I like JavaScript so far, and decided to use Node.js as my engine partly because of this , which claims that Node.js offers TCO. However, when I try to run this (obviously tail-calling) code with Node.js, it causes a stack overflow: function foo(x) { if (x == 1) { return 1; } else { return foo(x-1); } } foo(100000); Now, I did some digging, and I found this . Here, it seems to say I should write it like this: function* foo(x) { if (x == 1) { return 1; } else { yield foo(x-1); } } foo(100000); However, this gives me syntax errors. I've tried various permutations of it, but in all cases, Node.js

a tail-recursion version list appending function

不羁的心 提交于 2019-11-26 17:25:19
问题 i see several examples of implementing append an element to a list, but all are not using tail recursion . how to implement such a function in a functional style? (define (append-list lst elem) expr) 回答1: The following is an implementation of tail recursion modulo cons optimization, resulting in a fully tail recursive code. It copies the input structure and then appends the new element to it, by mutation, in the top-down manner. Since this mutation is done to its internal freshly-created data

What is the Scala annotation to ensure a tail recursive function is optimized?

廉价感情. 提交于 2019-11-26 17:17:57
I think there is @tailrec annotation to ensure the compiler will optimize a tail recursive function. Do you just put it in front of the declaration? Does it also work if Scala is used in scripting mode (for instance using :load <file> under REPL)? From the " Tail calls, @tailrec and trampolines " blog post: In Scala 2.8, you will also be able to use the new @tailrec annotation to get information about which methods are optimised. This annotation lets you mark specific methods that you hope the compiler will optimise. You will then get a warning if they are not optimised by the compiler. In

Node.js tail-call optimization: possible or not?

那年仲夏 提交于 2019-11-26 06:06:45
问题 I like JavaScript so far, and decided to use Node.js as my engine partly because of this, which claims that Node.js offers TCO. However, when I try to run this (obviously tail-calling) code with Node.js, it causes a stack overflow: function foo(x) { if (x == 1) { return 1; } else { return foo(x-1); } } foo(100000); Now, I did some digging, and I found this. Here, it seems to say I should write it like this: function* foo(x) { if (x == 1) { return 1; } else { yield foo(x-1); } } foo(100000);

What is the Scala annotation to ensure a tail recursive function is optimized?

一曲冷凌霜 提交于 2019-11-26 05:22:01
问题 I think there is @tailrec annotation to ensure the compiler will optimize a tail recursive function. Do you just put it in front of the declaration? Does it also work if Scala is used in scripting mode (for instance using :load <file> under REPL)? 回答1: From the "Tail calls, @tailrec and trampolines" blog post: In Scala 2.8, you will also be able to use the new @tailrec annotation to get information about which methods are optimised. This annotation lets you mark specific methods that you hope