2010-09-24 50 views
0

我是新来的lisp;我想知道这里有没有人能帮助我。为什么我得到这个lambda表达式错误,我能做些什么呢?

我有下面的代码片段:

(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 

可能有人请指出我的错误?谢谢!

回答

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)))))) 
+0

谢谢!缺少的T是罪魁祸首。 – 2010-09-24 22:52:07

+1

嵌套的COND是否可以做任何事情?看起来像将嵌套COND中的子句带出到主COND的子句级别将产生相同的结果。 – Vatine 2010-09-25 10:26:12

+0

@Vatine,这是一个很好的观察。我不想重写代码,但这是一回事。还有(cons foo nil)=(list foo),... – 2010-09-25 10:58:34

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喜欢将评论放在正确的位置。

+1

我觉得对于这个级别的评论的约定是'';;,这Emacs是幸福的缩进到您的代码相同的地方。 – Ken 2010-09-24 22:22:24