oop - CLOS:任意函数的方法组合
问题描述
在阅读 CLOS(Paul Graham 在ANSI Common Lisp中)时,我注意到有九个函数可以作为它的第二个defmethod
参数:
+
、and
、append
、list
、max
、min
、nconc
和。根据这个答案,它们被称为简单方法组合。or
progn
问题
为什么只有这九个?我不能将任意函数作为第二个参数传递的原因是什么?
我想要的例子
假设我定义xor
为
(defun xor (&rest args)
(loop for a in args counting (not (null a)) into truths
finally (return (= truths 1))))
(这当然可以改进)。我想定义几个描述衣服及其组合的类xor
:
(defgeneric looks-cool (x)
(:method-combination xor))
(defclass black-trousers () ())
(defclass quilt () ())
(defclass white-shirt () ())
(defclass hawaii-shirt () ())
(defmethod looks-cool xor ((tr black-trousers)) nil)
(defmethod looks-cool xor ((qu quilt)) t)
(defmethod looks-cool xor ((ws white-shirt)) nil)
(defmethod looks-cool xor ((hs hawaii-shirt)) t)
(defclass too-stiff (black-trousers white-shirt) ())
(defclass scottish (quilt white-shirt) ())
(defclass also-good (black-trousers hawaii-shirt) ())
(defclass too-crazy (quilt hawaii-shirt) ())
现在,如果这个编译(它没有),我将能够使用 Lisp 来指导我穿什么:
> (looks-cool (make-instance 'too-stiff))
NIL
> (looks-cool (make-instance 'scottish))
T
> (looks-cool (make-instance 'also-good))
T
> (looks-cool (make-instance 'too-crazy))
NIL
我很清楚这是一个相当人为的例子,没有实际意义。不过,我想知道背后是否有更深层次的原因,或者对九个功能的限制是否只是为了使实施更容易。
解决方案
使用标准的 Common Lisp 宏DEFINE-METHOD-COMBINATION来定义您自己的简单方法组合:
例子:
(define-method-combination xor :identity-with-one-argument t)
然后:
CL-USER 5 > (mapcar #'looks-cool (list (make-instance 'too-stiff)
(make-instance 'scottish)
(make-instance 'also-good)
(make-instance 'too-crazy)))
(NIL T T NIL)
如果我们看一下(define-method-combination xor :identity-with-one-argument t)
,它的名字有几个含义xor
:
它使用运算符
xor
——函数、宏或特殊形式——不仅允许使用函数。如果运算符名称应该与方法组合名称不同 -> 使用:operator
关键字来指定。它定义了一个名为
xor
. 此名称可用于defgeneric
.它定义了一个方法限定符
xor
。这可以用于defmethod
.
请注意,还可以使用 that 定义更复杂的方法组合DEFINE-METHOD-COMBINATION
。
推荐阅读
- django - 使用 SQL 数据库和 NoSQL 数据库
- sapui5 - 如何在 SAP UI5/Fiori 中创建 shell 插件应用程序?
- python - 我如何在 pexpect 中处理这个有问题的程序?
- c# - 为什么我不能使用 WorldToViewportPoint 进行 2D 鼠标移动
- javascript - 无法遍历数组。For循环失败
- sonarqube - SonarQube - 删除并重新创建同名的 SonarQube 项目
- react-native - 数组没有出现在 render()
- react-native - 抑制 React Native 应用程序中的启动日志记录
- html - 如何在 html 元素上为居中的背景图像添加填充
- r - rpart图中只有一片叶子形成