tail-recursion

iPhone dev -- performSelector:withObject:afterDelay or NSTimer?

To repeat a method call (or message send, I guess the appropriate term is) every x seconds, is it better to use an NSTimer (NSTimer's scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:) or to have the method recursively call itself at the end (using performSelector:withObject:afterDelay)? The latter doesn't use an object, b...

How to implement tail recursion for tree algorithms in prolog.

How could I convert the following into a tail recursive version. sum(void,0). sum(t(V,L,R),S) :- sum(L,S1), sum(R,S2), S is V + S1 + S2. It seems impossible to maintain a single accumulator as the branching is in 2^n magnitude. A possible solution would be to have the accumulator add a new accumulator to a list on each iterati...

Recursive function best practices; What are they?

Hello, What are some other language independent ways of designing recursive functions other than the typical: if (counter < 1) return output; else callSelf(); Do other methods even exist? Whenever viewing examples I always see a version of the code above. Thanks! :) ...

tail recursion sum, power, gcd in prolog?

hi guys, how can I accomplish this: Give a tail-recursive definition for each of the following predicates. power(X,Y,Z): XY=Z. gcd(X,Y,Z): The greatest common divisor of X and Y is Z. sum(L,Sum): Sum is the sum of the elements in L. so far I have done this but not sure if that's correct power(_,0,1) :- !. power(X,Y,Z) :- Y1 is Y -...

Limiting recursion depth in Scala

Can you always structure a recursive function for tail-call elimination? If not, what are other strategies to limit the stack size? For example: (inspired by http://stackoverflow.com/questions/1595427/break-or-shortcircuit-a-fold-in-scala) // Depth-first search of labyrinth, with large depth > stacklimit def search ( labyrinth: Search...

Does Scala support tail recursion optimization?

Does Scala support tail recursion optimization? ...

Why can't scalac optimize tail recursion in certain scenarios?

Why doesn't scalac (the Scala compiler) optimize tail recursion? Code and compiler invocations that demonstrates this: > cat foo.scala class Foo { def ifak(n: Int, acc: Int):Int = { if (n == 1) acc else ifak(n-1, n*acc) } } > scalac foo.scala > jd-gui Foo.class import scala.ScalaObject; public class Foo implements S...

Scala and tail recursion

There are various answers on Stack Overflow which explain the conditions under which tail recursion is possible in Scala. I understand the limitations and how and where I can take advantage of tail recursion. The part I don't understand is why the limitation to private or final methods exists. I haven't researched how the Scala compiler...

While or Tail Recursion in F#, what to use when?

Ok, only just in F# and this is how I understand it now : Some problems are recursive in nature (building or reading out a treestructure to name just one) and then you use recursion. In these cases you preferably use tail-recursion to give the stack a break Some languagues are pure functional, so you have to use recursion in stead ...

Are there problems that cannot be written using tail recursion?

Tail recursion is an important performance optimisation stragegy in functional languages because it allows recursive calls to consume constant stack (rather than O(n)). Are there any problems that simply cannot be written in a tail-recursive style, or is it always possible to convert a naively-recursive function into a tail-recursive on...

Tail Call Elimination in Clojure?

Can somebody rewrite this (plt) Scheme code into Clojure? (define (f n) (printf "(f ~a)~n" n) (g n)) (define (g n) (printf "(g ~a)~n" n) (h n)) (define (h n) (printf "(h ~a)~n" n) (f (+ n 1))) In such a way as to not collapse the procedures f, g, and h together and to allow the code to run indefinitely without cras...

Efficient recursion in functional programming vs. inefficient recursion in different paradigms

As far as I know recursion is very elegant but unefficient in OOP and procedural programming (see the wonderful "High Order perl", Mark Jason Dominus). I had some informations that in functional programming recursion is fast - keeping its elegance and simplicity. Could someone confirm and possibly amplify this? I am thinking in terms of...

Does this function use tail recursion?

I am wondering if oCaml optimizes this code to be tail recursive and if so does F#? let rec sum xs = match xs with | [] -> 0 | x :: xs' -> x + sum xs' ...

Recursive harmonic function returns NaN

Hi, I have written the following sample code to find the harmonic value of N. (1+1/2+1/3+...1/N). Read the comments in the code written in BOLD and help me to find why is this happening. #include <stdio.h> float harmonic(float n, float har) { if(n==0) { return 0; } if(n==1) { printf("%f\n", har+1.0f);***/...

Visual C++ Tail Call Optimization

According to answers to that question: Which, if any, C++ compilers do tail-recursion optimization? it seems, that compiler should do tail-recursion optimization. But I've tried proposed options and it seems that compiler can't do this optimization in case of template functions. Could it be fixed somehow? ...

Tail-recursive merge sort in OCaml

Hello world! I’m trying to implement a tail-recursive list-sorting function in OCaml, and I’ve come up with the following code: let tailrec_merge_sort l = let split l = let rec _split source left right = match source with | [] -> (left, right) | head :: tail -> _split tail right (head :: left) in _spli...

Tail-recursive pow() algorithm with memoization?

I'm looking for an algorithm to compute pow() that's tail-recursive and uses memoization to speed up repeated calculations. Performance isn't an issue; this is mostly an intellectual exercise - I spent a train ride coming up with all the different pow() implementations I could, but was unable to come up with one that I was happy with t...

Whats wrong with this method?

Here's the method: public static String CPUcolor () { System.out.println ("What color am I?") ; String s = getIns() ; System.out.println ("are you sure I'm "+s+"? (Y/N)") ; String a = getIns() ; while (!((a.equals ("y")) || (a.equals ("Y")) || (a.equals ("n")) || (a.equals ("N")))) { Syste...

Looking for an example of a typical tree-recursion turned into a tail-recursive form.

Anything like flatten, count-atoms, etc. over nested lists would do fine. BTW, I'm not interested in a CPS transformation or "pairs-trees". ...

Tail recursion in C++

Can someone show me a simple tail-recursive function in C++? Why is tail recursion better, if it even is? What other kinds of recursion are there besides tail recursion? ...