tail-call-optimization

Tail recursion in NodeJS

允我心安 提交于 2019-12-23 12:44:24
问题 So I recently came across case I needed to write code where callback calls itself and so on and wondered about NodeJS and tail-call support, so I found this answer https://stackoverflow.com/a/30369729 saying that yup, it's supported. So I tried it with this simple code: "use strict"; function fac(n){ if(n==1){ console.trace(); return 1; } return n*fac(n-1); } fac(5); Using Node 6.9.2 on Linux x64 and run it as node tailcall.js --harmony --harmony_tailcalls --use-strict and result was: Trace

Is there a technical reason that C# does not issue the “tail.” CIL instruction? [duplicate]

て烟熏妆下的殇ゞ 提交于 2019-12-22 01:23:48
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Why doesn't .net/C# eliminate tail recursion? Take the following C# code: using System; namespace TailTest { class MainClass { public static void Main (string[] args) { Counter(0); } static void Counter(int i) { Console.WriteLine(i); if (i < int.MaxValue) Counter(++i); } } } The C# compiler (mine anyway) will compile the Counter method into the following CIL: .method private static hidebysig default void Counter

Why does TCO require support from the VM?

孤街浪徒 提交于 2019-12-21 17:05:22
问题 Some VMs, most notably the JVM, are said to not support TCO. As a result, language like Clojure require the user to use loop recur instead. However, I can rewrite self-tail calls to use a loop. For example, here's a tail-call factorial: def factorial(x, accum): if x == 1: return accum else: return factorial(x - 1, accum * x) Here's a loop equivalent: def factorial(x, accum): while True: if x == 1: return accum else: x = x - 1 accum = accum * x This could be done by a compiler (and I've

Is this a tail call? (Javascript)

為{幸葍}努か 提交于 2019-12-21 14:47:53
问题 Supose you have a recursive function like: Blah.prototype.add = function(n) { this.total += n; this.children.forEach(function(child) { child.add(n); }); }; Is the child.add() a tail call? If not can it be written so it is? 回答1: Yes, it is a tail call: function(child) { child.add(n); // ^ tail } Yet nothing here is tail-recursive, because it's not a direct recursive call. Also this.children.forEach(…) is a tail call within the add method. However, the invocation of the callback within the

Explain to me what the big deal with tail call optimization is and why Python needs it

喜你入骨 提交于 2019-12-20 17:31:12
问题 So apparently, there's been a big brouhaha over whether or not Python needs tail call optimization. This came to a head when someone shipped Guido a copy of SICP because he didn't "get it." I'm in the same boat as Guido. I understand the concept of tail call optimization. I just can't think of any reason why Python really needs it. To make this easier for me to understand, could someone give me a snippet of code that would be greatly simplified using TCO? 回答1: Personally, i put great value on

Explain to me what the big deal with tail call optimization is and why Python needs it

女生的网名这么多〃 提交于 2019-12-20 17:29:38
问题 So apparently, there's been a big brouhaha over whether or not Python needs tail call optimization. This came to a head when someone shipped Guido a copy of SICP because he didn't "get it." I'm in the same boat as Guido. I understand the concept of tail call optimization. I just can't think of any reason why Python really needs it. To make this easier for me to understand, could someone give me a snippet of code that would be greatly simplified using TCO? 回答1: Personally, i put great value on

Does Swift implement tail call optimization? and in mutual recursion case?

烈酒焚心 提交于 2019-12-17 17:25:36
问题 In particular if I have the following code: func sum(n: Int, acc: Int) -> Int { if n == 0 { return acc } else { return sum(n - 1, acc + n) } } Will Swift compiler optimize it to a loop? And does it so in a more interesting case below? func isOdd(n: Int) -> Bool { if n == 0 { return false; } else { return isEven(n - 1) } } func isEven(n: Int) -> Bool { if n == 0 { return true } else { return isOdd(n - 1) } } 回答1: The best way to check is to examine the assembly language code generated by the

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

醉酒当歌 提交于 2019-12-17 09:09:13
问题 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? 回答1: Consider the following interaction with the REPL. First we define a class with a

Automatic TCO in Clojure

偶尔善良 提交于 2019-12-13 18:34:13
问题 Is there a way to define a function in Clojure that is automatically tail-call-optimized? e.g. (defrecur fact [x] (if (= x 1) 1 (* x (fact (dec x))))) would be translated internally to something like: (defn fact [x] (loop [n x f 1] (if (= n 1) f (recur (dec n) (* f n))))) Can you tell me if something like this already exists? 回答1: The short answer is "No". The slightly longer answer is that Clojure is deliberately designed to require explicit indication where Tail Call Optimisation is desired

Visual C++ Tail Call Optimization

强颜欢笑 提交于 2019-12-13 14:19:23
问题 According to answers to that question: Which, if any, C++ compilers do tail-recursion optimization? it seems, that compiler should do tail-recursion optimization. But I've tried proposed options and it seems that compiler can't do this optimization in case of template functions. Could it be fixed somehow? 回答1: I don't use the MS compilers, but GCC can certainly do tail-recursion optimisation for templates. Given this function: template <typename T> T f( T t ) { cout << t << endl; if ( t == 0