为什么我得到这个lambda表达式错误,我该怎么办呢?

时间:2010-09-24 21:17:13

标签: 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 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

有人可以指出我的错误吗?谢谢!

2 个答案:

答案 0 :(得分:4)

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

(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)))))) 

答案 1 :(得分:1)

你对cond的嵌套使用看起来很可疑。您可以使用if:

尝试以下表单
(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)))))

抱歉格式略有变化; emacs喜欢将评论放在右边。