tail-recursion

If I have an expression, which is partitially evaluable, is it a good idea to avoid tail-recursion?

Consider an haskell-expression like the following: (Trivial example, don't tell me what the obvious way is! ;) toBits :: Integral a => a -> [Bool] toBits 0 = [] toBits n = x : toBits m where (m,y) = n `divMod` 2 x = y /= 0 Because this function is not tail-recursive, one could also write: toBits :: Integral a => a -> [Bool] toB...

How to recognize what is, and what is not tail recursion?

Sometimes it's simple enough (if the self call is the last statement, it's tail recursion), but there are still cases that confuse me. A professor told me that "if there's no instruction to execute after the self-call, it's tail recursion". How about these examples (disregard the fact that they don't make much sense) : a) This one sho...

Optimizing tail-calls in C#

I've got a deeply recursive function that should in theory work well even with large inputs. The problem is at the time of writing I forgot that C# doesn't do tail-call optimization very well, if at all, so I get StackOverflowExceptions for any complex-enough input. The basic structure of the method is in two large methods, each calling ...

Optimizing a Haskell function to prevent stack overflows

I'm trying to create a function that recursively plays all possible games of tic-tac-toe using a genetic algorithm, and then returns a tuple of (wins,losses,ties). However, the function below always overflows the stack when called like this: scoreOne :: UnscoredPlayer -> [String] -> ScoredPlayer scoreOne player boards = ScoredPlayer (to...

Recursion overhead -- how serious is it?

Possible Duplicate: Is recursion ever faster than looping? I was first trained to program seriously in C, about 15 years ago. My employer wanted highly optimized code for computationally difficult tasks. I remember being advised more than once to rewrite recursions as loops, even at the expensive of readability, in order to ...

Binomial Coefficient using Tail Recursion in LISP

Hello, I'm a Computer Science student starting to learn LISP. I have been said to program a function to find C(n,k) using tail recursion, and I would greatly appreciate your help. I have reached this: (defun combinatorio-recursivo-cola (n k) (cond ((or (< n k) (< k 0)) NIL) ((or (= k 0) (= n k)) 1) (T (* (combinatorio...