Lisp 为什么我会得到这个lambda表达式错误,我能做些什么呢?

Lisp 为什么我会得到这个lambda表达式错误,我能做些什么呢?,lisp,common-lisp,Lisp,Common Lisp,我对lisp很陌生;我想知道这里是否有人能帮我 我有以下代码片段: (defun write-lookup (binding-list pattern fact) (cond ; No bindings have been stored ; Return the binding list with a new one! ((not binding-list) (cons (cons pattern fact) nil)) ; A

我对lisp很陌生;我想知道这里是否有人能帮我

我有以下代码片段:

(defun write-lookup (binding-list pattern fact)
(cond
        ; No bindings have been stored
        ; Return the binding list with a new one!
        ((not binding-list) (cons (cons pattern fact) nil))

        ; A list of bindings is being stored
        (cond

            ; The current binding matches
            ((equal (caar binding-list) pattern)
                ; Return the binding-list if value matches, nil else
                (if (compare pattern fact) binding-list nil))

            ; Recursively search the rest of the list for the binding
            ((cdr binding-list) (write-lookup (cdr binding-list) pattern fact))

            ; The list doesn't have the binding.
            ; Return the binding-list with the added pattern
            ( T (cons (cons pattern fact) binding-list)))))
当我尝试运行它时,我得到以下结果:

*** - SYSTEM::%EXPAND-FORM: (EQUAL (CAAR BINDING-LIST) PATTERN) should be a
  lambda expression

有人能指出我的错误吗?谢谢

您嵌套使用cond看起来可疑。您可以使用以下条件尝试以下表单:

(defun write-lookup (binding-list pattern fact) ; No bindings have been stored ; Return the binding list with a new one! (if (not binding-list) (cons (cons pattern fact) nil) (cond ; The current binding matches ((equal (caar binding-list) pattern) ; Return the binding-list if value matches, nil else (if (compare pattern fact) binding-list nil)) ; Recursively search the rest of the list for the binding ((cdr binding-list) (write-lookup (cdr binding-list) pattern fact)) ; The list doesn't have the binding. ; Return the binding-list with the added pattern (T (cons (cons pattern fact) binding-list))))) (定义写查找(绑定列表模式事实) ;未存储任何绑定 ;使用新的绑定列表返回绑定列表! (如有)(非约束性清单) (反对(反对模式事实)无) (续) ;当前绑定匹配 ((相等(caar绑定列表)模式) ;如果值匹配,则返回绑定列表,否则返回nil (if(比较模式事实)绑定列表nil)) ;递归搜索列表的其余部分以查找绑定 ((cdr绑定列表)(写查找(cdr绑定列表)模式事实)) ;列表没有绑定。 ;返回带有添加模式的绑定列表 (T(cons(cons模式事实)约束列表()())))
很抱歉,格式有轻微更改;emacs喜欢将注释放在右边。

您嵌套使用cond看起来很可疑。您可以使用以下条件尝试以下表单:

(defun write-lookup (binding-list pattern fact) ; No bindings have been stored ; Return the binding list with a new one! (if (not binding-list) (cons (cons pattern fact) nil) (cond ; The current binding matches ((equal (caar binding-list) pattern) ; Return the binding-list if value matches, nil else (if (compare pattern fact) binding-list nil)) ; Recursively search the rest of the list for the binding ((cdr binding-list) (write-lookup (cdr binding-list) pattern fact)) ; The list doesn't have the binding. ; Return the binding-list with the added pattern (T (cons (cons pattern fact) binding-list))))) (定义写查找(绑定列表模式事实) ;未存储任何绑定 ;使用新的绑定列表返回绑定列表! (如有)(非约束性清单) (反对(反对模式事实)无) (续) ;当前绑定匹配 ((相等(caar绑定列表)模式) ;如果值匹配,则返回绑定列表,否则返回nil (if(比较模式事实)绑定列表nil)) ;递归搜索列表的其余部分以查找绑定 ((cdr绑定列表)(写查找(cdr绑定列表)模式事实)) ;列表没有绑定。 ;返回带有添加模式的绑定列表 (T(cons(cons模式事实)约束列表()())))
很抱歉,格式有轻微更改;emacs喜欢将注释放在右边。

首先,您需要正确缩进代码:

(defun write-lookup (binding-list pattern fact)
  (cond
        ; No bindings have been stored
        ; Return the binding list with a new one!
   ((not binding-list) (cons (cons pattern fact) nil))

        ; A list of bindings is being stored
   (cond

            ; The current binding matches
    ((equal (caar binding-list) pattern)
                ; Return the binding-list if value matches, nil else
     (if (compare pattern fact) binding-list nil))

            ; Recursively search the rest of the list for the binding
    ((cdr binding-list) (write-lookup (cdr binding-list) pattern fact))

            ; The list doesn't have the binding.
            ; Return the binding-list with the added pattern
    (T (cons (cons pattern fact) binding-list)))))
典型的Lisp编辑器会在击键时为您执行此操作

现在您可以很容易地发现第一个COND缺少一个T子句。让我补充一点:

(defun write-lookup (binding-list pattern fact)
  (cond
        ; No bindings have been stored
        ; Return the binding list with a new one!
   ((not binding-list) (cons (cons pattern fact) nil))

        ; A list of bindings is being stored
   (t (cond

            ; The current binding matches
       ((equal (caar binding-list) pattern)
                ; Return the binding-list if value matches, nil else
        (if (compare pattern fact) binding-list nil))

            ; Recursively search the rest of the list for the binding
       ((cdr binding-list) (write-lookup (cdr binding-list) pattern fact))

            ; The list doesn't have the binding.
            ; Return the binding-list with the added pattern
       (T (cons (cons pattern fact) binding-list))))))
我还要将注释移出代码:

(defun write-lookup (binding-list pattern fact)
  (cond ((not binding-list)                          ; No bindings have been stored
         (cons (cons pattern fact) nil))             ; Return the binding list with a new one!
        (t                                           ; A list of bindings is being stored
         (cond ((equal (caar binding-list) pattern)  ; The current binding matches
                (if (compare pattern fact)           ; Return the binding-list if value matches, nil else
                    binding-list
                  nil))   
               ((cdr binding-list)                   ; Recursively search the rest list for the binding
                (write-lookup (cdr binding-list) pattern fact))
               (T                                    ; The list doesn't have the binding.
                (cons (cons pattern fact)            ; Return the binding-list adding the pattern
                      binding-list)))))) 

首先,您需要正确缩进代码:

(defun write-lookup (binding-list pattern fact)
  (cond
        ; No bindings have been stored
        ; Return the binding list with a new one!
   ((not binding-list) (cons (cons pattern fact) nil))

        ; A list of bindings is being stored
   (cond

            ; The current binding matches
    ((equal (caar binding-list) pattern)
                ; Return the binding-list if value matches, nil else
     (if (compare pattern fact) binding-list nil))

            ; Recursively search the rest of the list for the binding
    ((cdr binding-list) (write-lookup (cdr binding-list) pattern fact))

            ; The list doesn't have the binding.
            ; Return the binding-list with the added pattern
    (T (cons (cons pattern fact) binding-list)))))
典型的Lisp编辑器会在击键时为您执行此操作

现在您可以很容易地发现第一个COND缺少一个T子句。让我补充一点:

(defun write-lookup (binding-list pattern fact)
  (cond
        ; No bindings have been stored
        ; Return the binding list with a new one!
   ((not binding-list) (cons (cons pattern fact) nil))

        ; A list of bindings is being stored
   (t (cond

            ; The current binding matches
       ((equal (caar binding-list) pattern)
                ; Return the binding-list if value matches, nil else
        (if (compare pattern fact) binding-list nil))

            ; Recursively search the rest of the list for the binding
       ((cdr binding-list) (write-lookup (cdr binding-list) pattern fact))

            ; The list doesn't have the binding.
            ; Return the binding-list with the added pattern
       (T (cons (cons pattern fact) binding-list))))))
我还要将注释移出代码:

(defun write-lookup (binding-list pattern fact)
  (cond ((not binding-list)                          ; No bindings have been stored
         (cons (cons pattern fact) nil))             ; Return the binding list with a new one!
        (t                                           ; A list of bindings is being stored
         (cond ((equal (caar binding-list) pattern)  ; The current binding matches
                (if (compare pattern fact)           ; Return the binding-list if value matches, nil else
                    binding-list
                  nil))   
               ((cdr binding-list)                   ; Recursively search the rest list for the binding
                (write-lookup (cdr binding-list) pattern fact))
               (T                                    ; The list doesn't have the binding.
                (cons (cons pattern fact)            ; Return the binding-list adding the pattern
                      binding-list)))))) 

我认为这种级别的评论的惯例是
,Emacs很乐意将其缩进到与代码相同的位置。我认为这一级别注释的约定是
,Emacs很乐意将其缩进到与代码相同的位置。谢谢!失踪的T是罪魁祸首。嵌套的COND有什么作用吗?看起来,将嵌套COND中的子句带到主COND的子句级别将产生相同的结果。@Vatine,这是一个很好的观察结果。我不想重写代码,但那是一回事。另外(cons foo nil)=(list foo),…谢谢!失踪的T是罪魁祸首。嵌套的COND有什么作用吗?看起来,将嵌套COND中的子句带到主COND的子句级别将产生相同的结果。@Vatine,这是一个很好的观察结果。我不想重写代码,但那是一回事。另外(cons foo nil)=(list foo)。。。