tail-recursion

Convert multiple recursive calls into tail-recursion

限于喜欢 提交于 2019-12-12 20:34:16
问题 Just wondering if a function like this can be done tail-recursively. I find it quite difficult because it calls itself twice. Here is my non-tail-recursive implementation in javascript. (Yes I know most javascript engine doesn't support TCO, but this is just for theory.) The goal is to find all sublists of a certain length(size) of a given array(arr). Ex: getSublistsWithFixedSize([1,2,3] ,2) returns [[1,2], [1,3], [2,3]] function getSublistsWithFixedSize(arr, size) { if(size === 0) { return [

F#: Recursive Functions: Concatenating the common values between two lists

谁说我不能喝 提交于 2019-12-12 18:45:09
问题 let rec common a b = match isolate(a) b with | (x::xs,isolate(b)) -> if memberof(b x) then [x,common(xs b)] else common(xs b) Here is what I have right now, but I've been trying many different things. The memberof() takes a float list and a float and returns true if the float is a member of the float list. The memberof() function works. I can't figure out how to return the new list. I also have an isolate() function which will take a list and remove any duplicate floats within the list. This

Is is possible to convert this recursive function to tail-recursive using continuation passing style?

自作多情 提交于 2019-12-12 18:26:41
问题 I have recently written an ETL, which works just fine. I would like to remind myself how to use free monads, so would like to convert my ETL as such. Note: my intention here is not to write a better ETL, but to re-familiarize myself with free monads. In re-learing how free monads work, I got side tracked with the topic of this question. So I asked a related question some months ago. Someone commented that my recursive function could be made tail-recursive using continuation passing style. I

Tail recursion - Scala (any language else)

两盒软妹~` 提交于 2019-12-12 18:17:16
问题 i have a question about Tail recursion. As i know Tail recursion is when the last recursive call from function will deliver the result of the function. But when i have a function like this def func1(n: Int): Int = { if (n > 100) { n - 10 } else { func1(func1(n + 11)) } } would it be tail recursion ? For example func1(100) = func1(func1(111)) = func1(101) = 91 so the last recursive call would be func1(101) and it should deliver the results so that would be tail recursion right? I'm a little

What's the most efficient tail recursive prime verification function known?

那年仲夏 提交于 2019-12-12 13:19:52
问题 I was experimenting with meta programming to this point: // compiled on Ubuntu 13.04 with: // clang++ -O3 -ftemplate-depth-8192 -fconstexpr-depth=4096 -std=c++11 -stdlib=libc++ -lcxxrt -ldl compile-time-primes.cpp -o compile-time-primes // assembly output with: // clang++ -S -mllvm --x86-asm-syntax=intel -O3 -ftemplate-depth-8192 -fconstexpr-depth=4096 -std=c++11 -stdlib=libc++ -lcxxrt -ldl compile-time-primes.cpp -o compile-time-primes.asm #include <array> #include <iostream> template

How to Generate all k-elements subsets from an N-elements set recursively in Java

南楼画角 提交于 2019-12-12 13:07:43
问题 So I am stuck with this problem of trying to find all k-elements subsets from a given N-elements set. I know what the total number of k-subsets is using the formula C(n,k)=C(n-1, k-1)+C(n-1, k) and I also have an idea how to do it in a iterative manner, but I get stuck when I try to think of a recursive solution. Can anyone give me a hint? Thanks! 回答1: For each element of the set, take that element, then add in turn to that all (k-1) subsets of the remaining N-1 element set. "It was a dark

Tree to ordered list with tail recursion

送分小仙女□ 提交于 2019-12-12 09:57:11
问题 I am actually sitting over a hour on a problem and don´t find a solution for it. I have this data type: type 'a tree = Empty | Node of 'a * 'a tree * 'a tree And i have to find a function which converts a given tree in a ordered list. There is also no invariant like that the left child has to be less then the right. I already found a "normal" recursion solution but not a tail recursive solution. I already thought about to build a unordered list and sort it with List.sort , but this uses a

How to use OCaml's [@tailcall] annotation to assert tail-recursiveness?

落爺英雄遲暮 提交于 2019-12-12 09:54:12
问题 In OCaml, the [@tailcall] annotation lets you assert that a particular function-call is a tail call (and so hopefully your whole function is tail recursive). The question is: Where do I place the annotation exactly? Obvious, easy example: let rec f = function | 0 -> 0 | x -> (f [@tailcall]) (x - 1) (* works like a charm *) But I don't see how I can do it in "less obvious" places: let rec f = function | 0 -> 0 | x -> (|>) (x - 1) f (* uh? *) I can see from the assembly code that the latter

Why won't Scala optimize tail call with try/catch?

我怕爱的太早我们不能终老 提交于 2019-12-12 09:30:18
问题 In a recent StackOverflow answer, I gave the following recursive code: def retry[T](n: Int)(fn: => T): T = { try { fn } catch { case e if n > 1 => retry(n - 1)(fn) } } If I add the @tailrec annotation, I get: Could not optimize @tailrec annotated method retry: it contains a recursive call not in tail position. I was able to hack a tail-recursive alternative, but I still wonder why this didn't optimize. Why not? 回答1: To be tail-recursion optimized, this has to be transformed into something

Why can't scalac optimize tail recursion in certain scenarios?

送分小仙女□ 提交于 2019-12-12 08:35:08
问题 Why doesn't scalac (the Scala compiler) optimize tail recursion? Code and compiler invocations that demonstrates this: > cat foo.scala class Foo { def ifak(n: Int, acc: Int):Int = { if (n == 1) acc else ifak(n-1, n*acc) } } > scalac foo.scala > jd-gui Foo.class import scala.ScalaObject; public class Foo implements ScalaObject { public int ifak(int n, int acc) { return ((n == 1) ? acc : ifak(n - 1, n * acc)); } } 回答1: Methods that can be overridden can NOT be tail recursive. Try this: class