Search code examples
lisp

lisp do loop factorial code


(defun fact (n)
    (do
       ((i 1 (+ 1 i))
        (prod 1 (* i prod)))
       ((equal i n) prod)))

I have done the code above and when i try, fact(4), it give me ans is 6. I am not sure what is going wrong. Can anyone help me?


Solution

  • Change to

    (defun fact (n)
        (do
           ((i 1 (+ 1 i))
            (prod 1 (* i prod)))
           ((equal i (+ n 1)) prod)))
    

    Basically, you were doing one iteration less than necessary.