Questions tagged [tail-recursion]

Tail recursion is a recursive strategy in which a function does some amount of work, then invokes itself. The "tail" refers to the fact that the recursion is at the very end of the function. Many -- especially functional -- programming language compilers can turn these types of calls into iteration, meaning tail recursion in supported languages can be used without fear of a stack overflow, regardless of the number of calls.

Tail recursion is a recursive strategy in which a function does some amount of work, then invokes itself. The "tail" refers to the fact that the recursion is at the very end of the function. Many (especially functional language) compilers can turn these types of calls into iterative calls, meaning tail recursion can be used without fear of a stack overflow, regardless of the number of calls.

1348 questions
-2
votes
1 answer

Corecursion understanding

according to the wikipedia Corecursion - works synthetically, starting from a base case and building it up, iteratively producing data further removed from a base case. Do functional combinators make co-recursion ? like…
initmax
  • 395
  • 2
  • 10
-2
votes
1 answer

Avoiding 'too much recursion' error

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…
mwerschy
  • 1,698
  • 1
  • 15
  • 26
-3
votes
2 answers

How to prevent a recursion error implementing factorial in Python

I'm developing a math suite and am currently having issues implementing the recursive version of the factorial function. The issue is when I use it in my Bayesian combination function: C(n,k) = n! / k! * (n-k)! I've tested it independently and it…
CobraPi
  • 372
  • 2
  • 9
-3
votes
1 answer

Unexpected Python behaviour - function fails to terminate after return statement

I wrote a function to traverseMountain(altitude, stepsleft, substring) as helper function to part of a solution to one of the hackerrank problems. I consider it better practice to test functions as I write them, and this is where I am encountering…
-3
votes
1 answer

Creating sequences with every possible combination

I am trying to solve the following problem: For given n and α, determine the smallest number k for which n can be expressed as a sum of k numbers each of which is a perfect α-th power. My first idea was to create a sequence of length n, then using…
idknuttin
  • 105
  • 1
  • 5
-3
votes
2 answers

How can I turn this function to tail recursive

let rec hf0 k n = if n <= k then n else let rec loop i = if i > k then 0 else hf0 k (n - i) + loop (i + 1) in loop 1 I have no idea how to do it since there is a sum of recursive functions there. (Thanks @Jeffrey…
-3
votes
5 answers

Python Pure recursion - Divisor - One input

What is the recursive call (or inductive steps) for a function that returns the number of integers from 1 to N, which evenly divide N. The idea is to concieve a pure recursive code in python for this function. No 'for' or 'while' loops, neither…
HandyFrank
  • 13
  • 4
-3
votes
4 answers

How can you join int and list together?

But I still can't find the join function anywhere else on the internet. The main problem is that the head(items) is int and tail(items) is list, and I can't combine head and tail together. Here is the code I tried: def head(items): return…
-4
votes
1 answer

How can I replace the following code to non-recursive using a stack?

This is a finite-state machine: private int recursive(int rc, int pc, int sc) { for (;;) { Instruction actual = program[rc][pc]; switch (actual.type) { case FIRST: if (sc >= input.length || input[sc]…
-4
votes
2 answers

Why the final output of the recursive function is 5?

Why the output of the above code is 5 as the function should go to first 5-4-3-2 (since no output) loop decrements to 1 so return 12 so at last 12+1..... so the answer must not be 5 i guess ? using namespace std; int x() { } int reee(int n) { …
-5
votes
3 answers

How to develop recursive function to interesting game . Recursive Challenge

i am developing a kid´s game. It consists in 3 Tags 6 sacks Each sack has one secret fruit inside:Bananas or Tomatoes The first step is to reveal one sack, for example imagine we reveal sack 2 and it is a T(Tomato) Now the rule is that you have…
X.Otano
  • 2,079
  • 1
  • 22
  • 40
-5
votes
2 answers

recursively examining the first element of the list to see if list contains given element

I am stuck with this code because I cannot search for t list in t.contains method. public static boolean contains(T element, List t) { if (t.isEmpty()){ return false; } else if (t.contains(t) == element){ …
-6
votes
1 answer

Why does int main() { return main(); } cause stackoverflow and not tail recursion?

Compiled this on Windows using GCC. It crashed immediately with Exception Code: c00000fd. Edit : Tried compiling following code (for visible output) and it causes stackoverflow. #include int main(void) { printf("Hello World\n"); return…
Mark Evans
  • 974
  • 1
  • 11
  • 29
1 2 3
89
90