All Questions
72 questions
13
votes
1
answer
487
views
Optimization of tail recursion in R
Since version 4.4.0, R supports tail recursion through the Tailcall function. I automatically assumed that it means an improvement for codes that use tail recursion.
However, consider the following ...
0
votes
2
answers
147
views
Is there a difference in Haskell, regarding tail-recursion, between using guards that return boolean values and using (||) operators? [closed]
Let's use the following function as an example:
findWord :: [[Char]] -> [(Int, Int)] -> (Int, Int) -> String -> Bool
findWord _ _ _ [] = True -- Word was found
...
3
votes
1
answer
209
views
Tail recursion fibonacci derived from linear recursive version using Burstall & Darlington's folding/unfolding system
The inefficient (tree-recursive) fib(n) function calculates the n-th Fibonacci number. In Haskell:
fib 0 = 0
fib 1 = 1
fib n = fib (n - 1) + fib (n - 2)
Using the following identity:
gfib(n) = (fib(n)...
1
vote
3
answers
217
views
Can I use `recur` in this implementation of function composition in Clojure?
Consider this simple-minded recursive implementation of comp in Clojure:
(defn my-comp
([f]
(fn [& args]
(apply f args)))
([f & funcs]
(fn [& args]
(f (apply (apply my-...
1
vote
1
answer
1k
views
Does Dart feature Tail Call Optimization (TCO)?
I wanted to try out in Dart some algorithms and patterns from Functional Programming, but a lot of them rely heavily on recursion, which might incur in serious memory leaks without Tail Call ...
1
vote
1
answer
821
views
Tail call optimization (TCO) not working in Safari
According to ES6 compatibility table, Safari has tail call optimization feature. Tried it and it fails just like any other browser 😭. Am I missing something?
function factorial(n, r = 1n) {
...
3
votes
2
answers
562
views
How to implement append procedure using tail recursion in Scheme/Racket? [duplicate]
For a long time I'm trying to implement the append procedure in Racket using tail recursion(iterative).
The best solution I did so far is this:
(define (my-reverse lst)
(define (iter lst reversed)
...
2
votes
0
answers
49
views
Looking for an example to vividly illustrate that tail-recursive calls take less space than non-tail-recursive?
Software developers in functional programming languages have been told that tail-recursive calls are better than non-tail-recursive ones. A classic example in textbooks is the tail-recursive factorial ...
4
votes
2
answers
2k
views
Why use a helper function inside a recursive function?
In the book Functional Programming in Scala, in the context of explaining how recursion is often used in functional programming over imperative iteration, the authors show recursion via a factorial ...
1
vote
1
answer
95
views
Conversion of Looping to Recursive Solution
I have written a method pythagoreanTriplets in scala using nested loops. As a newbie in scala, I am struggling with how can we do the same thing using recursion and use Lazy Evaluation for the ...
-2
votes
1
answer
120
views
Why does the absence of an else block translate to Unit type return for a function?
I noticed there is a type mismatch caused in the line else if(r1 == 0 || divisors.tail.isEmpty || !divisors.tail.contains(r1)){newAcc}. Because there is no else clause to my if ... else if ...
def ...
2
votes
1
answer
186
views
How to map a tree in a stack safe manner?
There are different ways to map a tree recursively:
const reduceTree = (f, node) => {
const go = ([x, xs]) => f(x, xs.map(go));
return go(node);
};
const mapTree = (f, node) =>
...
12
votes
2
answers
2k
views
How to make tree mapping tail-recursive?
Suppose I have a tree data structure like this:
trait Node { val name: String }
case class BranchNode(name: String, children: List[Node]) extends Node
case class LeafNode(name: String) extends Node
...
-1
votes
1
answer
71
views
How to identify whether a problem can be solved with Tail-recursion or not in Scala
How can I identify whether a problem statement can be solved with tail recursion or not. Is there any characteristics of a problem by which one can identify that?
2
votes
1
answer
265
views
F# How to Flatten a Binary Search Tree
I have a tree, structured as :
type 'a Tree =| Leaf of 'a| Branch of 'a Tree * 'a Tree
I am using continuation-passing style tail recursion over my tree and trying to flatten it.
let rec loop tree ...