Binomial Coefficient using Tail Recursion in LISP

I want to program a function to find C(n,k) using tail recursion, and I would greatly appreciate your help.

I have reached this:

(defun tail-recursive-binomial (n k)
  (cond ((or (< n k) (< k 0)) NIL)
        ((or (= k 0) (= n k)) 1)
        (T (* (tail-recursive-binomial (- n 1) (- k 1)) (/ n k)))))

Using the following property of the binomial coefficients.

But I don't know how to make the recursive call to be the last instruction executed by each instance, since there the last one is the product. I have been trying it by using an auxiliary function, which I think is the only way, but I haven't found a solution.

5
задан jesusiniesta 17 February 2014 в 16:31
поделиться