首页 > 解决方案 > 使用局部变量并在嵌套的 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 ) ))

标签: common-lispnested-loops

解决方案


如果没有适当的缩进和代码格式,您不应该编写任何代码,也不应该编写 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))

推荐阅读