1,375 questions
1
vote
0
answers
65
views
Tail recursive MergeSort for Tuples in Scala
so im trying to make a mergesort for list with tuples, but no matter what i look at i dont see anything wrong with the implementation im doing. But still @tailrec is not being detected by Scala for ...
0
votes
3
answers
122
views
Tail-Recursive Binomial Coefficient Function in Java
I'm looking to implement a tail-recursive function to calculate the binomial coefficient of two numbers n and k. If k > n then 0 should be returned. Similarly if n == k or k == 0 it should return 1....
1
vote
1
answer
67
views
Do tail-recursive functions reuse a single environment in Scheme?
I'm currently taking an undergraduate course in functional programming, and we just learned about environments in Scheme. From what I understand, an environment is the context in which a function is ...
2
votes
1
answer
119
views
What is the exact compiler optimization applied here expect for tail recursion elimination?
I'm compiling a simple C program, implementing an inorder tree traversal function:
void inorderTraversal(struct TreeNode* root) {
if (root == NULL) {
return;
}
inorderTraversal(...
1
vote
0
answers
76
views
Why doesn't F# inline this inner function?
I have this active pattern:
[<return: Struct>]
let (|Integer|_|) self =
let rec loop acc (isNegative: bool) self =
match self with
| C(digit, tail) when (...
-1
votes
1
answer
96
views
What is the difference between head and tail recursion when reversing a linked list?
I am learning about different recursive approaches for reversing a linked list in C++. I have implemented both head and tail recursion methods, but I'm unsure about their differences and which one is ...
0
votes
0
answers
34
views
When combining iteration and recursion, how do I express the recursion via tail calls?
I am working on a practice problem and I have found a recursive solution. However, I can't figure out how to express the solution as tail recursion.
I think that the issue that I am having can be ...
3
votes
1
answer
107
views
Array and List recursion performance
I solving the problem of "Best-time-to-buy-and-sell-stock" at leetcode https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ with 2 approaches:
With first, I ...
2
votes
1
answer
129
views
Is `Pair` a valid instance of `MonadRec`?
In the paper Stack Safety for Free, Phil Freeman defines the MonadRec type class as follows.
class (Monad m) <= MonadRec m where
tailRecM :: forall a b. (a -> m (Either a b)) -> a -> m b
...
0
votes
1
answer
229
views
How can I create a tail recursive merge method in Scala on a self-referential tree structure (or is it even possible)?
UPDATE 2024.03.16: Provided code that produces the correct output, but is still not tail-recursive.
How can I create a tail recursive merge method in Scala on a self-referential tree structure (or is ...
0
votes
0
answers
85
views
XSLT recursion crashes after 1000 calls - how to transform it to DVC style?
Here is my function:
<func:function name="entry-check">
<xsl:param name="bunch"/>
<xsl:param name="history"/>
<xsl:param name="index&...
2
votes
3
answers
122
views
How are my recursive calls not tail calls here?
In the following code, all calls to retry get the warning "Recursive call is not a tail call":
private tailrec suspend fun <T> retry(numberOfRetries: Int, block: suspend () -> T): ...
0
votes
1
answer
55
views
Append a list on to another in oCaml
I need a tail recursive function that appends a list1 infront of list2.
I tried it this way
let rec append lst1 lst2 acc =
match lst1 with
| [] -> acc @ lst2
| hd::tl -> append lst1 tl (...
0
votes
1
answer
99
views
Confused about how tail recursion works?
I'm using an example of adding two linked lists together from leetcode. The lists are in reverse order and the resulting list must be reversed as well. My code is recursive and it's only faster than ...
3
votes
0
answers
196
views
Performance of deep recursion versus tail recursion
I have an algorithm using deep recursion (20 seconds). Perl complains and I shut it up with "no warnings 'recursion'". I change it to tail recursion (32 seconds) in the hope that it will be ...
1
vote
1
answer
61
views
Is an iterative process that returns a list with size that increases by iteration any different from an one that returns a scalar?
This actually comes from me initially misreading the text of Exercise 1.12.
The request is indeed to
Write a procedure that computes elements of Pascal's triangle by means of a recursive process.
I ...
-1
votes
2
answers
82
views
Recursion method is invoked even after loop condition is met
public int removeMin(Integer[] arr, int count) {
Integer[] tempArr = new Integer[arr.length -1];
int index = 0;
for (int i = 1; i<arr.length; i++) {
tempArr[index] = arr[i] ...
0
votes
2
answers
98
views
stack variable disappears at the last action of a function?
The advantage of tail recursion is that we call the function (recursion) again in the last action of the code, so the stack variables doesn't need to be saved.
So here do this will act the same? and I ...
1
vote
3
answers
76
views
Recursive iteration of an array
I have an array of classes:
const transferClasses = [
{
id: "c5d91430-aaab-ed11-8daf-85953743f5cc",
name: "Class1",
isTransfer: false,
children: [],
},
{
...
0
votes
1
answer
143
views
Is tail call (including tail recursion) compiler/implementation dependent?
Searching tail recursion on the internet I stumbled on How does compiler know whether the recursion is a tail recursion or not and how does it optimize tail recursion. If I understand correctly then ...
1
vote
1
answer
113
views
How to write a recursion function using 2 accumulators?
I'm new to Python and recursion is a foreign thing to me. For my assignment I have functions that involve tail recursion, a while loop, or a generator as specified by _t, _w, or _g if the function ...
1
vote
0
answers
75
views
gcc can't optimize tail recursion of a std::vector<int>, exit with code 139?
I have this program:
#include <iostream>
#include <vector>
int foo(std::vector<int> bar) {
if (bar[0] == 0)
return bar[0];
else
{
bar[0] -= 1;
...
0
votes
1
answer
270
views
How to remove last node of a linked list (Python)
The goal is to remove the last node of a linked list. (Python) The code that I have written only added to the list instead of taking the tail away. I'm not sure if the error is coming from this part ...
1
vote
2
answers
509
views
tail recursion to add element to end of list in Haskell
I wrote this simple function to append an element to the end of a list recursively:
--takes arguments x and lst and appends x to the end of list
append1::a->[a]->[a]
append1 x [] = x:[]
append1 ...
4
votes
2
answers
318
views
Automatically detect whether a Haskell function is tail recursive
I'm currently writing an auto-grader for a Haskell course. For the section on "tail-recursion", I need a way to automatically and safely detect whether a given Haskell function is tail-...