Почему я получаю эту ошибку лямбда-выражения, и что я могу с этим поделать? - PullRequest
0 голосов
/ 25 сентября 2010

Я довольно новичок в Лиспе;Мне было интересно, может ли кто-нибудь здесь помочь мне.

У меня есть следующий фрагмент кода:

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

4 голосов
/ 25 сентября 2010

Сначала вам нужно правильно сделать отступ в своем коде:

(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 сделает это за вас одним нажатием клавиши.отсутствует для первого КОНД.Позвольте мне добавить это:

(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 голос
/ 25 сентября 2010

Ваше вложенное использование 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 любит размещать комментарии справа.

...