tail-call-optimization

Is it possible to use continuations to make foldRight tail recursive?

╄→гoц情女王★ 提交于 2019-11-30 12:43:23
The following blog article shows how in F# foldBack can be made tail recursive using continuation passing style. In Scala this would mean that: def foldBack[T,U](l: List[T], acc: U)(f: (T, U) => U): U = { l match { case x :: xs => f(x, foldBack(xs, acc)(f)) case Nil => acc } } can be made tail recursive by doing this: def foldCont[T,U](list: List[T], acc: U)(f: (T, U) => U): U = { @annotation.tailrec def loop(l: List[T], k: (U) => U): U = { l match { case x :: xs => loop(xs, (racc => k(f(x, racc)))) case Nil => k(acc) } } loop(list, u => u) } Unfortunately, I still get a stack overflow for

Why does return/redo evaluate result functions in the calling context, but block results are not evaluated?

五迷三道 提交于 2019-11-30 09:26:46
Last night I learned about the /redo option for when you return from a function. It lets you return another function, which is then invoked at the calling site and reinvokes the evaluator from the same position >> foo: func [a] [(print a) (return/redo (func [b] [print b + 10]))] >> foo "Hello" 10 Hello 20 Even though foo is a function that only takes one argument, it now acts like a function that took two arguments . Something like that would otherwise require the caller to know you were returning a function, and that caller would have to manually use the do evaluator on it. Thus without

Differences between JVM implementations

╄→尐↘猪︶ㄣ 提交于 2019-11-30 06:22:16
问题 Where do JVM Implementations differ (except licensing)? Does every JVM implement Type Erasure for the Generic handling? Where are the differences between: JRockit IBM JVM SUN JVM Open JDK Blackdown Kaffe ..... Deals one of them with Tail-Call-Optimization? 回答1: JVM implementations can differ in the way they implement JIT compiling, optimizations, garbage collection, platforms supported, version of Java supported, etc. They all must meet set of features and behaviors so that it will execute

Achieving Stackless recursion in Java 8

爷,独闯天下 提交于 2019-11-29 21:38:48
How do I achieve stackless recursion in Java? The word that seems to come up the most is "trampolining", and I have no clue what that means. Could someone IN DETAIL explain how to achieve stackless recursion in Java? Also, what is "trampolining"? If you cannot provide either of those, could you please point me in the right direction (i.e., a book to read about it or some tutorial that teaches all of these concepts)? sdgfsdh A trampoline is a pattern for turning stack-based recursion into an equivalent loop. Since loops don't add stack frames, this can be thought of as a form of stackless

Is it possible to use continuations to make foldRight tail recursive?

血红的双手。 提交于 2019-11-29 18:26:45
问题 The following blog article shows how in F# foldBack can be made tail recursive using continuation passing style. In Scala this would mean that: def foldBack[T,U](l: List[T], acc: U)(f: (T, U) => U): U = { l match { case x :: xs => f(x, foldBack(xs, acc)(f)) case Nil => acc } } can be made tail recursive by doing this: def foldCont[T,U](list: List[T], acc: U)(f: (T, U) => U): U = { @annotation.tailrec def loop(l: List[T], k: (U) => U): U = { l match { case x :: xs => loop(xs, (racc => k(f(x,

Why does return/redo evaluate result functions in the calling context, but block results are not evaluated?

安稳与你 提交于 2019-11-29 14:29:23
问题 Last night I learned about the /redo option for when you return from a function. It lets you return another function, which is then invoked at the calling site and reinvokes the evaluator from the same position >> foo: func [a] [(print a) (return/redo (func [b] [print b + 10]))] >> foo "Hello" 10 Hello 20 Even though foo is a function that only takes one argument, it now acts like a function that took two arguments . Something like that would otherwise require the caller to know you were

Can/does the (forward) pipe operator prevent tail call optimization?

99封情书 提交于 2019-11-29 09:13:10
For a parameter optimization problem at work I wrote a genetic algorithm to find some good settings because a brute-force solution is unfeasible. Unfortunately, when I return in the morning, most of the time I'm presented with a StackOverflowException . I've been using F# for quite some time now so I'm aware of TCO and the need for functions with accumulator arguments and generally use that form. After a lot of searching I think I was able to nail to the code that triggered the exception: breedPopulation alive |> simulate (generation + 1) lastTime ewma breedPopulation generates a new

Why is this F# sequence function not tail recursive?

我们两清 提交于 2019-11-29 04:22:16
Disclosure: this came up in FsCheck, an F# random testing framework I maintain. I have a solution, but I do not like it. Moreover, I do not understand the problem - it was merely circumvented. A fairly standard implementation of (monadic, if we're going to use big words) sequence is: let sequence l = let k m m' = gen { let! x = m let! xs = m' return (x::xs) } List.foldBack k l (gen { return [] }) Where gen can be replaced by a computation builder of choice. Unfortunately, that implementation consumes stack space, and so eventually stack overflows if the list is long enough.The question is: why

Tail optimization guarantee - loop encoding in Haskell

若如初见. 提交于 2019-11-29 03:57:45
So the short version of my question is, how are we supposed to encode loops in Haskell, in general ? There is no tail optimization guarantee in Haskell, bang patterns aren't even a part of the standard (right?), and fold/unfold paradigm is not guaranteed to work in all situation. Here's case in point were only bang-patterns did the trick for me of making it run in constant space (not even using $! helped ... although the testing was done at Ideone.com which uses ghc-6.8.2). It is basically about a nested loop, which in list-paradigm can be stated as prod (sum,concat) . unzip $ [ (c, [r | t]) |

Does Java 8 have tail call optimization?

点点圈 提交于 2019-11-29 01:32:58
I tried digging on the web to get my question answered. I found some documents related to Project DaVinci . This is tagged to the JSR 292 which is related to including closures in the JVM. Did this project get realized and is it a part of Java 8? As far as I know Java 8 does not have tail call optimization. Afaik it isn't related to the actual compiler trick, because that one is simple, but to preserve a callstack for security purposes. But I guess it would be possible with a bytecode rewriter. Java does not support TCO at compiler level but it is possible to implement it with Java 8 using