Common lisp 使用局部变量并在嵌套的公共Lisp循环中返回它们
我有以下代码,嵌套循环有问题: 我的目标是为了学术目的实现CLOS多分派(多方法)。我有一个传递给泛型函数的参数列表。泛型函数(gf)包含一个方法列表。反过来,泛型函数中的每个方法都包含它所操作的参数所属的类(专门化器)列表。对于要应用的方法,传递给泛型函数的每个参数必须是泛型函数的方法列表中包含的方法中其各自专用程序的实例或子类。特别是,处理局部变量并在嵌套循环中返回它们是一个问题Common lisp 使用局部变量并在嵌套的公共Lisp循环中返回它们,common-lisp,nested-loops,Common Lisp,Nested Loops,我有以下代码,嵌套循环有问题: 我的目标是为了学术目的实现CLOS多分派(多方法)。我有一个传递给泛型函数的参数列表。泛型函数(gf)包含一个方法列表。反过来,泛型函数中的每个方法都包含它所操作的参数所属的类(专门化器)列表。对于要应用的方法,传递给泛型函数的每个参数必须是泛型函数的方法列表中包含的方法中其各自专用程序的实例或子类。特别是,处理局部变量并在嵌套循环中返回它们是一个问题 (defun compute-applicable-methods (gf &rest 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
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))
内部循环
不返回任何值,您将计数到不使用的局部变量匹配的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))
现在,内部循环
返回一个值,但我们不使用它。改进:
(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))
如果没有正确的缩进和代码格式,就不应该编写代码,也不应该编写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))
内部循环
不返回任何值,您将计数到不使用的局部变量匹配的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))
现在,内部循环
返回一个值,但我们不使用它。改进:
(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))
当(=matched_args(count args)看起来缺少一个
)时,
没有更多的pictureNeeds。matched_args的范围是什么?@Gakuo INTO引入的变量是封闭循环的局部变量。循环宏的设计目的不是检查子表达式的内容,它只在其主体内直接查找循环关键字,而不是嵌套循环内的关键字,因此它无法知道应该为内部匹配的参数累加器声明和管理变量。但是,您可以使用“finally(return matched_args)”,将值从一个循环返回到另一个循环。没有更多的pictureNeeds。当(=matched_args(count args)
看起来缺少一个)
时。matched_args的范围是什么?@Gakuo INTO引入的变量是封闭循环的局部变量。循环宏的设计目的不是检查子表达式的内容,它只在其主体内直接查找循环关键字,而不是嵌套循环内的关键字,因此它无法知道应该为内部匹配的参数累加器声明和管理变量。但是,您可以使用“finally(return matched_args)”将值从一个循环返回到另一个循环。非常有教育意义,Rainer!很高兴看!:)很有教育意义,雷纳!很高兴看!:)