I am trying to understand how various recursive functions can be converted to tail recursive. I've looked through the many examples of both fibonacci and factorial conversions to tail recursive and understand those but am having a tough time making the leap to a problem with a somewhat different structure....

Given the following definition for a (not binary) tree: sealed trait Tree[+A] case class Node[A](value: A, children: List[Node[A]]) extends Tree[A] object Tree {...} I have written the following fold method: def fold[A, B](t: Node[A])(f: A ⇒ B)(g: (B, List[B]) ⇒ B): B = g(f(t.value), t.children map (fold(_)(f)(g))) that can be...

I've stubled upon a function (here on SO) which writes a file, but makes sure to not overwrite a file: function writeFile(i){ var i = i || 0; var fileName = 'a_' + i + '.jpg'; fs.exists(fileName, function (exists) { if(exists){ writeFile(++i); } else { fs.writeFile(fileName); } }); } Now...

Not the best title for the question, I agree. But I couldn't come up with something else. Sorry. Writing a simple Singly Linked List code in Groovy, I want to add a method which takes in two lists and appends the right one to the left one. This is what...

I've seen around the following F# definition of a continuation-passing-style fibonacci function, that I always assumed to be tail recursive: let fib k = let rec fib' k cont = match k with | 0 | 1 -> cont 1 | k -> fib' (k-1) (fun a -> fib' (k-2)...

I'm learning F# and I'm building a minesweeper app. As part of that, I'm trying to have a method that detonates all adjacent mines if a mine is detonated, recursively. So if I have a grid like: | 0 | 1 | 2 | ------------------------ 0 | E-1 | M...

This question is inspired by some comments in an earlier Stackoverflow article on the same topic and also motivated by some code I'm writing as well. Given the example contained therein, I'm somewhat convinced that this pattern is tail recursive. If this is the case, how do I mitigate the...

given a matrix of int numbers, a row and col indexs (for a random cell that contains a number) and a new number, I need to recursively return the matrix- however now with all of the surrounding cells that matched the random cell number to the new one. for example:...

I'm not very experienced in JS. But as most people I sometimes have the need to add some extra functionality to a browser. When looking for answers to other questions I came upon this answer at SO. The answer and the responder are highly upvoted, and by SO standards it...

In order to demonstrate the effectiveness of tail recursion, I would like a way to access the depth of the call stack dynamically in Scheme. Is there a way to do this? If not, is there a way to do this in other major functional languages (OCaml, Haskell, etc.)?...

I have a function that calculates the binomial coefficient in Haskell, it looks like this: binom :: Int -> Int -> Int binom n 0 = 1 binom 0 k = 0 binom n k = binom (n-1) (k-1) * n `div` k Is it possible to modify it and...

i have a recursion problem. http://www.geeksforgeeks.org/print-increasing-sequences-length-k-first-n-natural-numbers/ But i see recursive calls to functions is not happening. what problem could be with the code below: def wrapper(n,k): list=[] for i in range(1,n+1): list.append(str(i)) print rec_seq(list,k,0,'') def rec_seq(list,k,i,prefix): #base case if k==0: return prefix else: for c in list[i:None]: newPrefix=prefix+c return rec_seq(list,k-1,i+1,newPrefix)...

The upcoming ES6 standard demands that an expression of the form new f(x) in tail position is a tail call. I can think of a number of artificial examples where this does matter, e.g. function Sum(n, m) { if (n === 0) { this.m = m; } else { return...

So, I'm fiddling with some basic maths, and I wanted a function to convert between bases. I wrote this function: (define (convert-base from to n) (let f ([n n]) (if (zero? n) n (+ (modulo n to) (* from (f (quotient n to))))))) Which works for all my personal tests...

I could really do with some help with tail call optimization in F#. I am trying to parse a tree like structure and perform a calculation on each leaf. The function I'm having problems with is calcLength type Location = float * float type Radius = float type Width =...

I am supposed to show that two algorithms execute identical statements in identical order. One is a tail recursive version of the other. They are written in Eiffel. tail_rec(x:instance_type):result_type is local y:instance_type; do if b(x) then Result :=c(x) else y:=d(x); Result:=tail_rec(y) end end Then the non-tail recursive version. non_rec(x:instance_type):result_type is...

Here is non tail recursive function alg :: Int -> Int alg n = if n<7 then n else alg(n-1) * alg(n-2) * alg(n-4) * alg(n-6) I've been stuck on this for a while, I get the basic idea of tail recursion, and how to do it for single call...

I'm trying to solve this problem. This function takes two parameters. The first is a function that returns a boolean value, and the second is a list of numbers. The function is supposed to remove the first value in the second parameter that returns true when run with the first...

I'm trying to write a regular recursion function and a tail-recursion function which takes four arguments and evaluates the sum of ax^2 + b for x from q to r. This is what I have so far and it is not working, can I get some information on what is...

I'm writing a function that does a set of transformations based on a declarative set of rules. The rules are a compile-time evaluated collection (no function calls or anything). They will contain hundreds of elements. The basic layout looks like this: (defn dostuff-with-rules [stuff] (let [rules [["foo"] ["bar"] ["baz"] ...]...

I am confused by following code: the code is artificial, but still I think it is tail recursive. The compiler does not agree and produces an error message: @annotation.tailrec def listSize(l : Seq[Any], s: Int = 0): Int = { if (l.isEmpty) { None.getOrElse( return s ) } listSize(l.tail, s...

I am trying the coding bat problem repeatFront: Given a string and an int n, return a string made of the first n characters of the string, followed by the first n-1 characters of the string, and so on. You may assume that n is between 0 and the length...

Let us consider following pseudo code QuickSelect(A, k) let r be chosen uniformly at random in the range 1 to length(A) let pivot = A[r] let A1, A2 be new arrays # split into a pile A1 of small elements and A2 of big elements for i = 1 to...

Here is a question from previous HackerEarth Challenge - Roy has a matrix of size NxN. Rows and Columns are numbered from 0 to N-1. jth column of ith row contains absolute difference between i and j. In other words, Matrix[i][j] = abs(i-j) where 0 ≤ i, j < N....

we can solve this problem in, say C, using a static variable, like in the snippet below (like the function found in this page). static int i = 0; if(head == NULL) return; getNthFromLast(head->next, n); if(++i == n) {THIS IS THE NTH ELEM FROM LAST DO STUFF WITH IT. }...