0 Replies - 1107 Views - Last Post: 07 September 2010 - 07:08 AM

#1 captainhampton  Icon User is offline

  • Jawsome++;
  • member icon

Reputation: 14
  • View blog
  • Posts: 548
  • Joined: 17-October 07

[Lisp] Factorial (Tail Recursion)

Posted 07 September 2010 - 07:08 AM

Description: Let n be an arbitrary natural number for the function.Tail recursive factorial
; Calculates the factorial using tail recursion
; Author : Vincent Russo

(defun tailfact(n &optional (intermediate 1))
              (if (= n 1)
                  (return-from tailfact intermediate))
              (tailfact(1- n) (* n intermediate)))
			  
;For stack optimization			  
(compile 'tailfact)

Is This A Good Question/Topic? 0
  • +

Page 1 of 1