Questions tagged [recursion]

Questions about objects such as functions, algorithms or data structures that are expressed using "smaller" instances of themselves.

595 questions
8
votes
2 answers

Difference between Tail-Recursion and structural recursion

Is there any difference between structural-recursion and Tail-recursion or they both are same? I see that in both of these recursions , the recursive function is called on the subset of the orignal items.
Khan Saab
  • 253
  • 2
  • 8
4
votes
1 answer

Recursion Davis' Staircase understanding

I'm going through a hackerrank challenge on recursion https://www.hackerrank.com/challenges/ctci-recursive-staircase and I'm having difficulty understanding how the solution below solves the problem. Here's the tree (the recursion calls) like you…
user75512
  • 41
  • 1
  • 2
3
votes
1 answer

Is there any practical application for arrays that contain themselves?

I'm reading the introduction to Godel, Escher, Bach and and there's heavy discussion of mathematical sets which contain themselves. Being a far better programmer than mathematician, I put it into terms I'm more comfortable with and started to think…
davidlav
  • 143
  • 3
3
votes
0 answers

Axiomatisation in the presence of recursion

I read Klaus Havelund's thesis on the Fork Calculus: http://havelund.com/Publications/thesis.ps He develops the Fork calculus for reasoning about concurrent functional programs, the motivation being Concurrent ML. In chapter 5, he writes: First, we…
Gergely
  • 379
  • 1
  • 9
2
votes
2 answers

Tasks in which recursion is either the fastest or only way to produce a result

I've just finished studying recursion at university. One thing that stood out for me however was that in both the lectures and in the practical we completed, all the tasks we were asked to do could be performed faster, and in less code, using…
2
votes
1 answer

Integer log2 as a catamorphism?

Recursion schemes are structured methods for expressing recursive functions, of increasing interest due to their ubiquity in functional programming. For example, catamorphisms (familiar in the guise of 'fold' on lists) can be defined for all…
NietzscheanAI
  • 817
  • 4
  • 10
2
votes
1 answer

Decimal to Binary converter with a flaw

Hi I wrote the following recursive decimal to binary function while studying for Andela. def binary_converter(q): if q>255 or not isinstance(q, int) or q<0: return "Invalid input" if q==0: return '0' elif q!=0: q,…
Jonathan
  • 123
  • 5
2
votes
0 answers

How to prove νX. A × X ≅ (μX. 1 + X) -> A?

How can we prove Stream A = νX. A × X is isomorphic to Nat -> A = (μX. 1 + X) -> A ? In programming sense, Stream A can be seen as a function from Nat to A, and I can write isomorphisms between them. But how can this be proven mathematically? I…
inamiy
  • 121
  • 3
2
votes
1 answer

Is this an example of Tail Recursion

As I have read in this answer: What is tail recursion? tail recursion is a special case of recursion where the calling function does no more computation after making a recursive call. Here after the recursive call we just assign the result to…
2
votes
1 answer

How does this recursive algorithm work?

One question from the Grokking Algorithms book: Implement a max([]int) function, returning the biggest element in the array. Here's my solution in Golang (adapted from the Python solution in the book): func maxNumber(arr []int) (r int) { // base…
2
votes
1 answer

Is there purely recursive functions?

Is there any problem that can be only solved with recursion, and not with iteration? (haven't been able to find anything online). If there isn't any, is there a reason why? Thanks in advance!
1
vote
1 answer

Towers of Hanoi Algorithm "Using Auxiliary" Peg

I'm trying to fully understand how the recursive Towers of Hanoi Algorithm works and to implement it in code. Something that keeps throwing me of is the use of phrases like "move n-1 discs from A to B using C." I understand that the peg which is…
Robin Andrews
  • 255
  • 2
  • 14
1
vote
1 answer

Converting Recursion into Into Iteration

While reading Code Complete second edition, I came across this line: You can do anything with stacks and iteration that you can do with recursion. Is this true? or there are functions, that must be implemented recursively, and can't be done…
Bite Bytes
  • 249
  • 1
  • 7
1
vote
3 answers

Value returned by recursive function

(pseudocode) f(x,y,z) { if (x < y) { return 1 + f(x + 1, y, z) } else if (y < z) { return 2 + f(x, y + 1, z) } else { return z } } The function call f(12,14,20) returns $40$. Why is…
Marcel
  • 133
  • 4
1
vote
3 answers

Having trouble on logic behind recursion

I am struggling to write my own recursive function.I understand how to find the base case but I cant find easily the pattern on the relationship between 2 complicated cases.Do you know any website where I can practice that?
Cerise
  • 153
  • 5
1
2 3