使用局部变量并在嵌套的 Common Lisp 循环中返回它们
working with local variables and returning them in nested Common Lisp loops
我有以下代码,嵌套循环有问题:
我的目标是为学术目的实施 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
没有 return 任何值,您计入一个您不使用的局部变量 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
return 是一个值,但我们不使用它。改进:
(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))
我有以下代码,嵌套循环有问题: 我的目标是为学术目的实施 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
没有 return 任何值,您计入一个您不使用的局部变量 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
return 是一个值,但我们不使用它。改进:
(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))