Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [recursion]

The tag has no usage guidance.

-1
votes
1answer
109 views

What happens when loops are compiled or interpreted?

I have came across a concept in recursion which says that when loops are compiled or interpreted then they get converted to recursive functions. If it is true how it takes place ?
2
votes
1answer
130 views

How to optimize reusing a large std::unordered_map as a temporary in a frequently called function?

Simplified question with a working example: I want to reuse a std::unordered_map (let's call it umap) multiple times, similar to the following dummy code (which does not do anything meaningful). How ...
1
vote
2answers
162 views

Pass-Through parameters in recursive code

When writing large amounts of recursive code (for valid reasons), I have come across many parameters that are not used in specific functions, but are still needed for a subset of all of the functions. ...
-1
votes
1answer
127 views

True or false about recursion

I am working on a quiz for a computer science course. I would like to check that the following statement is false: A recursive call may never generate more than one recursive call for the ...
0
votes
1answer
99 views

Express unique recursive task system

I would like to express a task as either itself or a container for other tasks (recursively). The problem is that each task has to be one of two fundamental types: a goal or a routine. And, children ...
1
vote
2answers
415 views

Patterns for tracking state in recursive Haskell code

A frequent pattern in my Haskell code is element-wise recursion for transformation of a list with some carried state generated using the data in the list. Usually, this looks something like this: ...
0
votes
1answer
85 views

Generating combinations without getting stuck in recursive calls

This question is about figuring the design of a recursive function that changes the state of a group of elements by processing one of them at a time, with the goal of reaching a desired state. The ...
0
votes
0answers
257 views

Algorithm for recursive division maze generation on hexagonal grid

I'm currently writing a book on procedural generation and I'm writing a chapter about maze generation. I've done some searching but can only find recursive division demonstrated on a square grid. I ...
5
votes
0answers
111 views

Why does GHC represent recursion in Haskell with recursive binders instead of a fixpoint operator?

GHC's Core data type represents recursion with recursive binders in the Let constructor; as I understand it, all let expressions in Haskell are effectively let rec expressions. Why does GHC use this ...
0
votes
0answers
66 views

Loops can be used for everything recursion can do, however Recursion cannot do everything you can do with a loop [duplicate]

My professor used this on the test I took. The answer was True, but he never explained what we can do with loops that we can't with recursion.
3
votes
1answer
109 views

What is best practice for handling mapping arbitrary depth in arrays?

I have a set of data: id | name | parentid ------------------------ 1 | parent | 0 2 | child | 1 3 | child | 1 4 | parent | 0 5 | child | 4 6 | subchild | 5 7 | child | 4 ...
0
votes
1answer
49 views

Question about memory handling of trees and certain sorts on large data sets.

If data structures such as trees and certain sorts(quick sort, merge sort) work using recursive algorithms and recursive algorithms take up a lot of memory space in the stack and can only work on a ...
-1
votes
1answer
88 views

Inserting at head for a recursively defined linked list

This article states However, this design does run into problems with some of the methods of the MutableSequence ABC. Most notably, insert and __ delitem __ can't operate on position 0, because ...
0
votes
1answer
737 views

An elegant way to detect the end of an asynchronous recursive file search?

Given a recursive subroutine in single threaded environment which starts numerous asynchronous I/O operations and registers callback functions for each of them. This callbacks will be called on the ...
0
votes
1answer
216 views

Recursion explanation

I am not able to understand this basic recursion example. When I executed this code. The output was 43210. Should not the output be 44444, as the value of num is updated to 4 to the latest recursive ...
0
votes
1answer
112 views

Memoization in case of recursive interdefined functions in Haskell/Functional Programming?

I was reading Memoization with recursion which tells how for a recursively defined function fun we can do memoization by: -- Memoization memoize f = (map f [0 ..] !!) -- Base cases g f 0 = 0 g f 1 = ...
11
votes
4answers
864 views

What are the considerations to determine whether you can use recursion to solve a problem?

Sometimes in interviews, I may use recursion to solve a problem (such as adding 1 to an infinite precision integer), or when the problem presents itself suitable to use recursion. Sometimes, it might ...
0
votes
1answer
504 views

Eliminating recursion

It is easy to eliminate tail recursion. There are few curious cases where the not-tail recursion can also be eliminated. Exhibit 1: Fibonacci numbers. A naive recursive solution fib(n) if (n &...
1
vote
1answer
943 views

How do I represent a recursive process down a tree in a sequence diagramm?

I'm working on a sequence diagram for a layered system that has a tree hierarchy. Now I have a process that works itself recursively down the tree. Meaning calling the same function on a child object. ...
2
votes
2answers
1k views

Efficiency considerations: nested loop vs recursion

I would consider myself an intermediate Python programmer. One of my recent challenges was creating a list of all possible solutions to a given Countdown problem. Without getting into too much detail,...
0
votes
2answers
191 views

How do I write an algorithm to solve a set of 3 symbolic equations

Replace each symbol (letter) with a number so that the equations hold across all 3 equations. Solution should be able to solve the general case of any 3 equations. Can assume 2 terms summed to an ...
0
votes
1answer
77 views

Why isn't this combinatorial solution equivalent to the recursive solution for finding the number of “paths”?

Here's the problem: Given an m x n array, get the number of different paths from the top left corner to the bottom right corner, if you can only move down, right, and diagonally down & right. ...
4
votes
4answers
6k views

Why does recursion return the first call in the stack and not the last?

I cant for the life of me figure out why this returns 0 rather than 5. i keeps getting incremented before it hits the last return statement, however it always returns 0, from the first call in the ...
4
votes
1answer
703 views

How does K&R's qsort work?

In the recursion section of K&R's ANSI C book, they demonstrate a version of quicksort [that] is not the fastest possible, but it's one of the simplest. --The C Programming Language (ANSI ...
0
votes
2answers
302 views

How to decide how to build a recursive function

Sometimes when you're coding you're on a problem which can be solved with some recursive method. What is for you the best way to detect when the recursion is a good way to solve a problem and how to ...
2
votes
2answers
1k views

Designing the recursive solution

I understand the recursion and find it useful and intuitive while solving problems on trees but for many other problems recursion never fails me to leave puzzled. Recently I was solving the following ...
3
votes
1answer
319 views

Are recursions intrinsically less safe than iterations?

Java doesn't have a predefined recursion depth limit. As a result the recursion below (a dummy method that returns the value) throws java.lang.StackOverflowError after 62844 (with static) and 14002 (...
0
votes
1answer
6k views

how is stack and heap are assigned to each processes?

How multiples processes are stored in the main memory , i understand every process will be divided into the equal size pages and will be stored in the frames of main memory. if whole main memory is ...
37
votes
11answers
14k views

Is a while loop intrinsically a recursion?

I wondered whether a while loop is intrinsically a recursion? I think it is because a while loop can be seen as a function that calls itself at the end. If it is not recursion, then what is the ...
3
votes
3answers
744 views

How to mentally keep track of recursion

I have great trouble doing recursion, especially trying to visualize it. A great example is the aging wine problem described in the top answer of this link: https://www.quora.com/Are-there-any-good-...
3
votes
5answers
6k views

Recursion in Merge Sort algorithm. How is it obvious to use this type of recursion?

I dont want to put too much code so I'll just put the code that involves recursion. This algorithm is fairly well known so I think everybody knows the basic code. void mergeSort(int array[], int l, ...
0
votes
1answer
68 views

Reflexive calls of objects in the same hierarchy

I am not sure how to phrase this. I believe this should have been asked somewhere, but I am unable to find it because I don't know the keywords. Basically, I have some types like this: interface Foo ...
-2
votes
1answer
1k views

How does this recursive function exit? [closed]

In the merge_sort part, there are multiple calls to the same function within the recursive. How are the other two executed when the definition holds that till a condition is false, the recursion does ...
0
votes
2answers
1k views

Implementing map with tail recursion

I am trying to solve this exercise. It is about reimplementing the map function in haskell for learning purpose. I found a solution which doesn't browse all the elements of the list (simple linked ...
2
votes
0answers
125 views

String Pattern Matching from Lookup Table - Non-Exponential Solution?

Given the problem... Given a String comprising of non-alhpabetical symbols, and a Lookup Table where a subset of those symbols may equate to a single alphabetical character, output all possible ...
1
vote
1answer
154 views

Understanding recursive solution of some algorithm

Most of the time we need to understand someone else' code for example I am studying Graph Algorithms from Sedgewick's online resources, the particular code example is taken from cycle detection ...
0
votes
0answers
47 views

Reverse lookup of data based on user input

I have an array in the format: $array = array( 0 => array( 'name' => 'Item 1', 'level' => 1, 'points' => 10 ), 1 => array( 'name' => '...
0
votes
1answer
257 views

Achieving permutation using recursion [closed]

Though I like recursions and understand it, I fail to master it! I am stuck with a problem which I know that recursion can solve, but I do not know how. I have an array of strings like: "hundred", "...
-2
votes
1answer
231 views

For loop and recursion for a new shell in C [closed]

I code a new shell in C, that could be done in several ways: Flex/bison, finite state machine, abstract syntax tree or just a tokenizer in C. Now I've written a for-loop that changes the condition of ...
3
votes
1answer
104 views

Term for the current iteration of a recursive function call

Is there a standard or widely accepted term for referring to a variable within the current call of a recursive function where the last value of the same variable is passed as an argument? I am trying ...
1
vote
1answer
178 views

Complexity of recursive calls

my brain is a little stuck as I am trying to compile an example for my blog. I am presenting an algorithm that draws a number from an array, adds it to a sum and calls itself recursively. func solve(...
10
votes
3answers
414 views

Algorithm to generate all sets of m points in n x n x n cubic lattice which are unique under symmetry

I am implementing an algorithm which is going to be quite computationally complex, and want to try to make sure I'm not doing unnecessary work. There is an n x n x n cubic lattice, e.g. if n=2 this ...
8
votes
1answer
252 views

Finding all possible ways of inserting a pattern into a string

I've been thinking about this problem for a while, and I can only find a recursive solution but I'm feeling that there is a dynamic programming way to do it, I just can't figure it out. Is this a ...
2
votes
1answer
310 views

Enumerating the primitive recursive functions

How can I enumerate (by expression tree size, for example) all of the primitive recursive functions that map natural numbers to natural numbers in a traditional programming language like C? For ...
-2
votes
1answer
232 views

Problem on recursion

void function(int x){ if(x<=0) return; function(x--); } This is a recursion function which is called with the value of x = 20. The Recursive call will take place in this way ...
7
votes
3answers
413 views

What would be a good approach to generate a tree of folders?

Say I have an array of strings, like this: var folders = new[] { "Foo", "Bar", "Foo\Bar" "Foo\Bar\Baz" }; And that I have an object that represents a folder - something like this: ...
3
votes
3answers
579 views

What can qualify for potential tail call recursion (TCO) optimization or tail recursion elimination (TRE)

Short question is: what can qualify for potential tail call recursion optimization (TCO) or tail recursion elimination (TRE) if the compiler or interpreter supports it. The summary of this question ...
0
votes
1answer
608 views

False recursion vs. true recursion [closed]

I have been warned by my professor to avoid "false recursion", where a function behaves recursively, but does no useful recursive work. I believe I understand recursion, but would like to double ...
124
votes
11answers
22k views

Is there anything that can be done with recursion that can't be done with loops?

There are times where using recursion is better than using a loop, and times where using a loop is better than using recursion. Choosing the "right" one can save resources and/or result in fewer lines ...
0
votes
3answers
603 views

Is recursion a bad idea for large input sizes due to the limited call stack size? [duplicate]

Call stack size in JavaScript: Three results: - Node.js: 11034 - Firefox: 50994 - Chrome: 10402 It seems like it would be a bad idea to use recursion for things like BST insertion, because there's a ...