Disclaimer: this post is pure rant

Whenever recursion is introduced in some language, the favorite example function is "factorial", right ?

For instance, in Lisp:

(defun factorial (n)
     (cond ((<= 'n 0)  1)
           (t (* 'n (factorial (- 'n 1))))))

But this is bad programming. For two reasons:

First, it's inefficient. This function is as easily implemented iteratively - which is far more efficient.

Second, and worse. This is completely useless. Come on, how many times did YOU need to compute a factorial in your code ? And even if you did... factorial of what ? 5, 6, 10 ? For instance, the factorial if the measly 42 is 1405006117752879898543142606244511569936384000000000 - hardly a number you will practically need. So, why bother ? A look-up table will do the work best !

Ranting, sorry :-)


comments powered by Disqus