common-lisp - 使用局部变量并在嵌套的 Common Lisp 循环中返回它们
问题描述
我有以下代码并且嵌套循环有一个问题:我的目标是实现 CLOS 多调度(多方法)用于学术目的。我有一个传递给通用函数的参数列表。通用函数 (gf) 包含一个方法列表。反过来,泛型函数中的每个方法都包含其操作的参数所属的类(专用器)列表。对于适用的方法,传递给泛型函数的每个参数必须是泛型函数的方法列表中包含的方法中其各自特化器的实例或子类。特别是,使用局部变量并在嵌套循环中返回它们是一个问题。
(defun compute-applicable-methods (gf &rest args)
(loop for method in (generic-function-methods gf)
do (loop for specializer in (method-specializer method)
for arg in args
counting (instancep arg specializer) into matched_args
)
when (= matched_args (count args)
collect method ) ))
解决方案
如果没有适当的缩进和代码格式,您不应该编写任何代码,也不应该编写 Lisp 代码。使用 Lisp,您也没有任何借口,因为 IDE 会为您缩进代码。
缩进固定:
(defun compute-applicable-methods (gf &rest args)
(loop for method in (generic-function-methods gf)
do (loop for specializer in (method-specializer method)
for arg in args
counting (instancep arg specializer) into matched_args
)
when (= matched_args (count args)
collect method ) ))
代码的格式仍然很奇怪。改进:
(defun compute-applicable-methods (gf &rest args)
(loop for method in (generic-function-methods gf)
do (loop for specializer in (method-specializer method)
for arg in args
counting (instancep arg specializer) into matched_args)
when (= matched_args (count args)
collect method)))
您可以看到的第一件事是该函数=
没有正确的参数列表。collect
并且method
不应该是=
:
(defun compute-applicable-methods (gf &rest args)
(loop for method in (generic-function-methods gf)
do (loop for specializer in (method-specializer method)
for arg in args
counting (instancep arg specializer) into matched-args)
when (= matched-args (count args))
collect method))
内部LOOP
不返回任何值,您计入一个matched-args
不使用的局部变量。
如果您不使用该变量,让我们将其删除:
(defun compute-applicable-methods (gf &rest args)
(loop for method in (generic-function-methods gf)
do (loop for specializer in (method-specializer method)
for arg in args
count (instancep arg specializer))
when (= matched-args (count args))
collect method))
现在内部LOOP
返回一个值,但我们不使用它。改进:
(defun compute-applicable-methods (gf &rest args)
(loop for method in (generic-function-methods gf)
for matched-args = (loop for specializer in (method-specializer method)
for arg in args
counting (instancep arg specializer))
when (= matched-args (count args))
collect method))
推荐阅读
- docker - 通过像事件对象一样传递 API Gateway 在本地调用 Lambda docker 容器
- python - POST 方法不允许用于简单的查找网页
- java - 线程“主”core.ops.SignatureUnificationException 中的异常:找不到名为:ClassName 的类。找不到非主类
- html - 如何使 div 边框具有弹性但具有最小高度?
- android - 无法安装 @react-native-firebase/app 面向 compileDebugJavawithJavac FAILED
- python - 如何使用 for 循环压缩一系列 seaborn 散点图?
- c - 结构内的字符串 malloc 溢出
- macos - 鱼未知命令:__fish_pwd
- node.js - 如何从 TSX 组件中的输入标记中获取值,并在不同目录中的 Node JS 文件中使用该值?
- kaggle - Kaggle——新菜单防止右键单击和复制图形