tail-recursion

Generate tail call opcode

99封情书 提交于 2019-12-17 06:12:40
问题 Out of curiosity I was trying to generate a tail call opcode using C#. Fibinacci is an easy one, so my c# example looks like this: private static void Main(string[] args) { Console.WriteLine(Fib(int.MaxValue, 0)); } public static int Fib(int i, int acc) { if (i == 0) { return acc; } return Fib(i - 1, acc + i); } If I build it in release and run it without debugging I do not get a stack overflow. Debugging or running it without optimizations and I do get a stack overflow, implying that tail

Generate tail call opcode

爱⌒轻易说出口 提交于 2019-12-17 06:12:09
问题 Out of curiosity I was trying to generate a tail call opcode using C#. Fibinacci is an easy one, so my c# example looks like this: private static void Main(string[] args) { Console.WriteLine(Fib(int.MaxValue, 0)); } public static int Fib(int i, int acc) { if (i == 0) { return acc; } return Fib(i - 1, acc + i); } If I build it in release and run it without debugging I do not get a stack overflow. Debugging or running it without optimizations and I do get a stack overflow, implying that tail

empty list on recursive function

烂漫一生 提交于 2019-12-14 02:35:59
问题 Im trying do some work on Project Euler for fun, but I got stuck now on an problem and want to move on but cant seem to get my function working. Im trying to get count the primefactors of a given integer. The function works on smaller numbers such as 13195: > primeFactor 13195 [29,13,7,5] But when I run a bigger number such as 600851475143: > primeFactor 601851475143 [] This seems really weird to me. I know haskell is a lazy language, but I don´t think it should be that lazy... primeFactor' :

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

Avoiding 'too much recursion' error [closed]

巧了我就是萌 提交于 2019-12-13 10:29:48
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . Basically what I'm trying to do boils down to function a() { // Do stuff that waits for things etc b(a); } function b(f) { f() } function a() { b(a); }; function b(f) { f(); }; a() That will cause a too much recursion error after a while though. Apparently javascript doesn't support tail-recursion

How to implement flatten list in prolog ,with tail recursion?

淺唱寂寞╮ 提交于 2019-12-13 08:59:58
问题 How to implement flatten list in prolog ,with tail recursion ? This is code for flatten/2 with simple recursion (that is mean without back-tracking): flatten([], []). flatten([L|Ls], FlatL) :- !, flatten(L, NewL), flatten(Ls, NewLs), append(NewL, NewLs, FlatL). flatten(L, [L]). ?- flatten([1, [2,3], [4]], X). X=[1,2,3,4]. I'm trying to do the same algorithm but with tail recursion (Accumulator). For exemple, the predicate sum/2 returns the addition of all member of the list, with backtracking

How does `process.nextTick` keep my stack from blowing up?

泪湿孤枕 提交于 2019-12-13 05:52:16
问题 I've stubled upon a function (here on SO) which writes a file, but makes sure to not overwrite a file: function writeFile(i){ var i = i || 0; var fileName = 'a_' + i + '.jpg'; fs.exists(fileName, function (exists) { if(exists){ writeFile(++i); } else { fs.writeFile(fileName); } }); } Now there is an interesting comment below which says: Minor tweak: Since JavaScript doesn't optimize tail recursion away, change writefile(++i) to process.nextTick(function(i) {writefile(++i);}); that will keep

Find Path to the First Occurrence in a Nested Data Structure

久未见 提交于 2019-12-13 02:46:23
问题 Consider the following numbers nested by vectors in a tree-structure (def tree [7 9 [7 5 3 [4 6 9] 9 3] 1 [2 7 9 9]]) My goal is to find the path to the first even number that can be found by traversing the tree: In the upper example this would be 4, the path from the root to this node would be [2 3 0] (def result [2 3 0]) I got some difficulties writing a function tho archive this. However, the following function finds the first even number, not its path: (defn find-even [x] (if (vector? x)

Is this tail-recursion?

房东的猫 提交于 2019-12-13 00:53:10
问题 I wrote a function to calculate the power of an integer then take modulus. I wonder if what I did is tail recursion : int takeModulusTAILRECURSION(int coef, int x, int n, int module){ int result = 0; if ( n > 0 ) result = takeModulusTAILRECURSION( mod(coef * x, module), x , n - 1, module ); else if ( n == 0) result = mod ( coef , module ); return result; } //take modul of an integer ( both negative and positive ) int mod(int x, int modul){ while ( x < 0) x += modul; return x % modul; } Here