My implementation for factorial in Lisp. This is a response to a post found here.

Also, it is tail recursive.

(defun fact (n)
     (labels ((rec (n acc)

         (if (<= n 1)

             acc

             (rec (1- n ) (* acc n)))))

     (rec n 1)))


ps: Factorial in Haskell seems easier though

fact 0 = 1
fact n = n * fact (n-1)


or

fact = product . enumFromTo 1

 


Comments




Leave a Reply