tail-call-optimization

How 'smart' is GCC's Tail-Call-Optimisation?

人走茶凉 提交于 2019-12-12 16:00:12
问题 I just had a discussion where the following two peices of C code were being discussed: For-Loop: #include <stdio.h> #define n (196607) int main() { long loop; int count=0; for (loop=0;loop<n;loop++) { count++; } printf("Result = %d\n",count); return 0; } Recursive: #include <stdio.h> #define n (196607) long recursive(long loop) { return (loop>0) ? recursive(loop-1)+1: 0; } int main() { long result; result = recursive(n); printf("Result = %d\n",result); return 0; } On seeing this code, I saw

Does MATLAB perform tail call optimization?

流过昼夜 提交于 2019-12-12 08:22:40
问题 I've recently learned Haskell, and am trying to carry the pure functional style over to my other code when possible. An important aspect of this is treating all variables as immutable, i.e. constants. In order to do so, many computations that would be implemented using loops in an imperative style have to be performed using recursion, which typically incurs a memory penalty due to the allocation a new stack frame for each function call. In the special case of a tail call (where the return

What reasoning lead to `Sequence expression containing recursive definition is compiled incorrectly`

一曲冷凌霜 提交于 2019-12-10 19:23:26
问题 The question Stack overflow despite tail call position but only in 64-bit lead to uncovering a bug in the F# compiler. After reading the answer I am curious as to the reasoning that lead to finding the bug as I would like to better improve my skills at resolving problems and understanding TCO. 回答1: My reasoning was something like this: Talking about "tail calls" when looking at a computation expression can be misleading - in general there really isn't such a thing (see e.g. this other answer

How do I jump out of a function in Lisp?

蹲街弑〆低调 提交于 2019-12-10 17:47:00
问题 Is it possible in (Common) Lisp to jump to another function instead of call another? I mean, that the current function is broken and another is called, without jumping back through thousands of functions, as if I'd decide myself if tail call optimization is done, even if it is not the tail. I'm not sure if "(return-from fn x)" does, what I want. Example: (defun fn (x) (when x (princ x) (jump 'fn (cdr x))) (rest)) 'jump' should be like calling the following function without saving the position

What's some simple F# code that generates the .tail IL instruction?

可紊 提交于 2019-12-10 13:01:59
问题 I'd like to see the .tail IL instruction, but the simple recursive functions using tail calls that I've been writing are apparently optimized into loops. I'm actually guessing on this, as I'm not entirely sure what a loop looks like in Reflector. I definitely don't see any .tail opcodes though. I have "Generate tail calls" checked in my project's properties. I've also tried both Debug and Release builds in Reflector. The code I used is from Programming F# by Chris Smith, page 190: let

Is it possible to force tail call optimization on GCC/Clang?

大城市里の小女人 提交于 2019-12-10 12:56:56
问题 I'm trying to write a program in functional style with C as much as possible. I know fine compilers like GCC/Clang do tail call optimization silently, but it's not guaranteed. Is there any option to force tail call optimization on the compilers? (Of course when only it's called at the end of itself) 回答1: Clang is not doing any optimisations at all. There is an LLVM pass tailcallelim which may do what you want (but it is not guaranteed). You can run it separately with opt . 回答2: I don't think

Why does tail call optimization need an op code?

萝らか妹 提交于 2019-12-10 01:06:44
问题 So I've read many times before that technically .NET does support tail call optimization (TCO) because it has the opcode for it, and just C# doesn't generate it. I'm not exactly sure why TCO needs an opcode or what it would do. As far as I know, the requirement for being able to do TCO is that the results of a recursive call are not combined with any variables in the current function scope. If you don't have that, then I don't see how an opcode prevents you from having to keep a stack frame

Are programs in functional languages more likely to have stack overflows?

我怕爱的太早我们不能终老 提交于 2019-12-07 07:41:49
问题 I am starting to learn ocaml, and am really appreciating the power of recursion in the language. However, one thing that I am worried about is stack overflows. If ocaml uses the stack for function calls, won't it eventually overflow the stack? For example, if I have the following function: let rec sum x = if x > 1 then f(x - 1) + x else x;; it must eventually cause a stack-overflow. If I was to do the equivalent thing in c++ (using recursion), I know that it would overflow. So my question is,

Pass-by-reference hinders gcc from tail call elimination

为君一笑 提交于 2019-12-07 02:05:32
问题 See BlendingTable::create and BlendingTable::print . Both have the same form of tail recursion, but while create will be optimized as a loop, print will not and cause a stack overflow. Go down to see a fix, which I got from a hint from one of the gcc devs on my bug report of this problem. #include <cstdlib> #include <iostream> #include <memory> #include <array> #include <limits> class System { public: template<typename T, typename... Ts> static void print(const T& t, const Ts&... ts) { std:

Tail recursion issue

依然范特西╮ 提交于 2019-12-06 06:43:51
问题 We were experimenting with parallel collections in Scala and wanted to check whether the result was ordered. For that, I wrote a small function on the REPL to do that check on the very large List we were producing: def isOrdered(l:List[Int]):Boolean = { l match { case Nil => true case x::Nil => true case x::y::Nil => x>y case x::y::tail => x>y & isOrdered(tail) } } It fails with a stackOverflow (how appropriate for a question here!). I was expecting it to be tail-optimized. What's wrong? 回答1: