4

考虑 Common Lisp 中的以下代码:

  (defun range (max &key (min 0) (step 1))
    (loop for n from min below max by step
          collect n))
  (reduce #'* (range 61 :min 1))

这给出了预期的 bignum 值 60!,即

8320987112741390144276341183223364380754172606361245952449277696409600000000000000

但是,以下代码(不包含我可以看到的任何浮点强制)会产生浮点答案:

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

  (fact 60)
  8.32098711274139e+81

问题是“为什么?” 和“我怎样才能fact在 SBCL(Steel-Bank Common Lisp)中编写一个简单的递归,产生一个 bignum 结果?”

4

1 回答 1

5

The coercion happens on the emacs side. ob-lisp.el calls read on the result. Try evaluating this on the *scratch* buffer to see for your self

(read "8320987112741390144276341183223364380754172606361245952449277696409600000000000000")
于 2015-12-04T16:54:22.467 回答