Questions tagged [recursion]
For questions about recursion, the practice of calling a method or function from within itself.
167 questions
1
vote
9
answers
3k
views
can every program still be written without recursion or cyclic calls?
I'm wondering if it was theoretically possible for a compiler to determine maximum stack depth at compile time if a limitation was placed on the program. The limitation being that you can't use ...
13
votes
8
answers
6k
views
How do compilers work in a language that doesn't allow recursion?
I'm recently learning the programming language, and I wonder how compilers work when the language itself does not allow recursion, like how the compiler or the runtime checkers makes sure that there ...
0
votes
1
answer
100
views
How do I create a new class or struct with property of its own type?
I have been trying to figure out if I can create a class or struct with a property that is of the same class or struct. An example would be
struct Number {
var Value: Int = 0
var Rate: Number(...
1
vote
2
answers
1k
views
Calling recursive method in a loop - Backtracking
I'm confused about a matter that I've been unable to figure out. I'm doing some leetcode problems. In backtracking problems, sometimes we use loop within our recursive method to call the recursion but ...
23
votes
8
answers
12k
views
Can I use additional parameters in recursion problems?
Okay, I was being interviewed at a company and the interviewer asked me a recursion problem. It was an online interview, so, he had set up the problem statement and a function signature on CodeSandbox ...
-3
votes
2
answers
226
views
Recursion + Stack/Queue usage in practical life implementation [closed]
I am very new to Data Structures and Algorithms. I want to use recursion + stack/queue in a grocery store inventory program that I am making. However, I can't come up with any good ways of using ...
-2
votes
1
answer
3k
views
Iterative Sorts vs. Recursive Sorts
Naive sorts like Bubble Sort and Insertion Sort are inefficient and hence we use more efficient algorithms such as Quicksort and Merge Sort. But then, these two sorts are recursive in nature, and ...
0
votes
2
answers
360
views
Representing a recursive Structure in OOP
I want to represent an Edifact message in an OOP Language. An Edifact message has the following structure:
message := [ ( segment | segmentgroup ) ]
segmentgroup : = [ ( segment | segmentgroup ) ...
1
vote
6
answers
1k
views
If the whole point of recursion is to break the problem into multiple smaller problems, what if those problems were solved in parallel?
So I was studying here and I was thinking if the whole point of recursion is to break the problem into multiple smaller problems, what if those problems were solved in parallel? A quick search lead me ...
2
votes
1
answer
4k
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
votes
1
answer
664
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 ...
2
votes
2
answers
2k
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:
...
12
votes
1
answer
386
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
1
answer
64
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 ...
0
votes
1
answer
1k
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
1
answer
226
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 = ...
0
votes
1
answer
2k
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 &...
2
votes
2
answers
3k
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
1
answer
88
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.
...
5
votes
1
answer
2k
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 C)...
2
votes
2
answers
2k
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
1
answer
12k
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 ...
3
votes
3
answers
3k
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-...
0
votes
1
answer
107
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
...
0
votes
2
answers
3k
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 ...