为什么会出现此lambda表达式错误,我该怎么办? [英] Why am I getting this lambda expression error, and what can I do about it?

查看:98
本文介绍了为什么会出现此lambda表达式错误,我该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Lisp很陌生;我想知道这里是否有人可以帮助我.

I'm pretty new to lisp; I was wondering if anyone here could help me out.

我有以下代码段:

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

当我尝试运行它时,我得到以下信息:

When I try to run it, I get the following:

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

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

Could someone please point out my error? Thanks!

推荐答案

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

First you need to indent your code properly:

(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编辑器会在您击键时为您完成此操作.

A typical Lisp editor will do that for you on a keystroke.

现在,您可以轻松发现第一个COND缺少T子句.让我添加它:

Now you can easily spot that a T clause is missing for the first COND. Let me add it:

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

我还将注释从代码中移出

I would also move the comment out of the code:

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

这篇关于为什么会出现此lambda表达式错误,我该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆