Skip to content

Instantly share code, notes, and snippets.

@JuanitoFatas
Created June 27, 2013 06:53
Show Gist options
  • Save JuanitoFatas/5874468 to your computer and use it in GitHub Desktop.
Save JuanitoFatas/5874468 to your computer and use it in GitHub Desktop.
Common Lisp 代碼例子:Factorial
(defun factorial (n)
(declare (optimize speed))
(labels ((fact (n acc)
(declare (type fixnum n)
(type bignum acc))
(if (<= n 0)
acc
(fact (the fixnum (1- n))
(the bignum (* n acc))))))
(fact n 1)))
Copy link

ghost commented Aug 5, 2015

When acc is not a bignum, will encouter another error

CL-USER> (defun factorial (n)
  (declare (optimize speed))
  (labels ((fact (n acc)
    (declare (type fixnum n)
             (type bignum acc))
    (if (<= n 0)
        acc
        (fact (the fixnum (1- n))
              (the bignum (* n acc))))))
    (fact n 1)))
; in: DEFUN FACTORIAL
;     (FACT N 1)
; 
; caught WARNING:
;   Constant 1 conflicts with its asserted type BIGNUM.
;   See also:
;     The SBCL Manual, Node "Handling of Types"
; 
; compilation unit finished
;   caught 1 WARNING condition
FACTORIAL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment