根据另一个闭包来定义闭包

s.y*_*ari 2 closures common-lisp

我有两个相互补充的闭包.我想用另一个来定义一个.例如,假设我们more-than定义了如下函数

(defun more-than (v) #'(lambda (x) (> x v)))

CL-USER> (funcall (more-than 5) 7)
T
CL-USER> (funcall (more-than 5) 3)
NIL
Run Code Online (Sandbox Code Playgroud)

我们想less-than-or-equal用上面的闭包来定义它的补充.这似乎并不像上面那样简单,因为我的尝试到目前为止还没有奏效.有人可以指出一个可能的解决方案,或者告诉我这是否是普通模式(即,不是独立于第一个闭包定义第二个闭包).

这是我的两个尝试不起作用

;; compile time error
(defun less-than-or-equal (v)
  #'(lambda (x) #'(not (more-than v))))
;; returns nil for every comparison
(defun less-than-or-equal (v)
  #'(lambda (x) (not (more-than v))))
Run Code Online (Sandbox Code Playgroud)

jki*_*ski 7

正如我在评论中提到的,您可以使用它COMPLEMENT来补充函数:

(defun less-than-or-equal (v)
  (complement (more-than v)))
Run Code Online (Sandbox Code Playgroud)

在你的尝试中,你NOT用来否定返回的函数MORE-THAN,而不是调用函数并否定结果.X根本没用过.要解决它,你需要做

(defun less-than-or-equal (v)
  ;; The #' is not necessary here
  (lambda (x) (not (funcall (more-than v) x))))
Run Code Online (Sandbox Code Playgroud)

但使用COMPLEMENT更好.