9

来自 Clojure 的背景,我被它的前置/后置条件作为合同设计的基础所吸引:

;; sqr.clj

(defn sqr [n]
  {:pre  [(not= 0 n) (number? n)]
   :post [(pos? %) (number? %)]}
  (* n n))

(sqr 10)
;=> 100

(sqr 0)
; Assertion error

Common Lisp 中是否有类似的前/后功能和/或更全面的按合同设计的库?

谢谢

4

2 回答 2

8

编写一个可以像这样使用的宏相对简单:

(defun sqr (n)
  (with-dbc-checked
     (:pre  ((not (zerop n)) (numberp n))
      :post ((plusp %) (numberp %)))
    (* n n)))

对于 CLOS 通用函数,请参见此处: http: //www.muc.de/~hoelzl/tools/dbc/dbc-intro.html

顺便说一句,从这段代码中可以看出,CL 和 Clojure 之间可以进行零代码交换,而无需完全重写任何东西。

于 2010-09-24T14:36:32.870 回答
1

你可以断言:

(defun sqr (n)
  (assert (and
           (not (zerop n))
           (numberp n)))
  (* n n))

不知道确切的帖子部分是做什么的。:)

于 2010-09-24T15:44:56.927 回答