tail-recursion

C# compilation with tail recursive optimization?

被刻印的时光 ゝ 提交于 2019-12-17 18:57:20
问题 Based on the rich wealth of stackoverflow, I've been getting on and off answers on whether the tail recursive optimization is done to specifically c# code. A few of the questions appeared to talk about Speculation of the optimization in newer versions of .net that were being released Building application as a x64bit application to achieve the optimization Switching from a debug build to a release build in Visual Studio to achieve the optimization No optimization at all and that the microsoft

Implementing a tail recursive version of quicksort-like function in F#/OCaML

故事扮演 提交于 2019-12-17 18:46:01
问题 Is it possible to implement a tail recursive version of the quick sort algorithm (via the continuation pattern)? And if it is, how would one implement it? Normal (not optimized) version: let rec quicksort list = match list with | [] -> [] | element::[] -> [element] | pivot::rest -> let ``elements smaller than pivot``, ``elements larger or equal to pivot``= rest |> List.partition(fun element -> element < pivot) quicksort ``elements smaller than pivot`` @ [pivot] @ quicksort ``elements larger

Avoiding stack overflow (with F# infinite sequences of sequences)

时间秒杀一切 提交于 2019-12-17 18:27:09
问题 I have this "learning code" I wrote for the morris seq in f# that suffers from stack overflow that I don't know how to avoid. "morris" returns an infinite sequence of "see and say" sequences (i.e., {{1}, {1,1}, {2,1}, {1,2,1,1}, {1,1,1,2,2,1}, {3,1,2,2,1,1},...}). let printList l = Seq.iter (fun n -> printf "%i" n) l printfn "" let rec morris s = let next str = seq { let cnt = ref 1 // Stack overflow is below when enumerating for cur in [|0|] |> Seq.append str |> Seq.windowed 2 do if cur.[0]

How to implement a stack-safe chainRec operator for the continuation monad?

耗尽温柔 提交于 2019-12-17 17:05:46
问题 I am currently experimenting with the continuation monad. Cont is actually useful in Javascript, because it abstracts from the callback pattern. When we deal with monadic recursion, there is always the risk of a stack overflow, because the recursive call isn't in tail position: const chain = g => f => k => g(x => f(x) (k)); const of = x => k => k(x); const id = x => x; const inc = x => x + 1; const repeat = n => f => x => n === 0 ? of(x) : chain(of(f(x))) (repeat(n - 1) (f)); console.log(

How can I implement a tail-recursive list append?

爷,独闯天下 提交于 2019-12-17 16:22:03
问题 A simple append function like this (in F#): let rec app s t = match s with | [] -> t | (x::ss) -> x :: (app ss t) will crash when s becomes big, since the function is not tail recursive. I noticed that F#'s standard append function does not crash with big lists, so it must be implemented differently. So I wondered: How does a tail recursive definition of append look like? I came up with something like this: let rec comb s t = match s with | [] -> t | (x::ss) -> comb ss (x::t) let app2 s t =

Does PHP optimize tail recursion?

倖福魔咒の 提交于 2019-12-17 10:53:41
问题 I wrote a small piece of code that I believe should have succeeded if tail recursion was optimized, however it blew up the stack. Should I conclude PHP does not optimize tail recursion? function sumrand($n,$sum) { if ($n== 0) { return $sum; } else { return (sumrand($n-1,$sum+rand(0,1))); } } echo sumrand(500000,0)."\n"; 回答1: Here are the generated opcodes for that (sorry for the strange representation): Global -------------------------------------------------------------------------------

While or Tail Recursion in F#, what to use when?

混江龙づ霸主 提交于 2019-12-17 10:38:14
问题 Ok, only just in F# and this is how I understand it now : Some problems are recursive in nature (building or reading out a treestructure to name just one) and then you use recursion. In these cases you preferably use tail-recursion to give the stack a break Some languagues are pure functional, so you have to use recursion in stead of while-loops, even if the problem is not recursive in nature So my question : since F# also support the imperative paradigm, would you use tail recursion in F#

While or Tail Recursion in F#, what to use when?

让人想犯罪 __ 提交于 2019-12-17 10:38:00
问题 Ok, only just in F# and this is how I understand it now : Some problems are recursive in nature (building or reading out a treestructure to name just one) and then you use recursion. In these cases you preferably use tail-recursion to give the stack a break Some languagues are pure functional, so you have to use recursion in stead of while-loops, even if the problem is not recursive in nature So my question : since F# also support the imperative paradigm, would you use tail recursion in F#

Are functions in JavaScript tail-call optimized?

南楼画角 提交于 2019-12-17 10:23:40
问题 I have been trying to understand Tail call optimization in context of JavaScript and have written the below recursive and tail-recursive methods for factorial() . Recursive: function factorial (n) { if (n < 2) { return 1; } else { return n * factorial(n-1); } } Tail-recursive: function factorial (n) { function fact(n, acc) { if (n < 2) { return acc; } else { return fact(n-1, n * acc); } } return fact(n, 1) } But I am not sure if the tail-recursive version of the function will be optimised by

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