带有循环和条件的 Lisp 代码中的语法错误

Syntax error in Lisp code with loop and conditions

以下代码中的语法错误是什么?

(defun getchoice3 ()
  (let ( (choice 1) )
    (format t  "~%Enter a number (1-5): ")
    (loop for choice = (or (parse-integer (prompt-read "Choice: ") :junk-allowed t) 0) do
      while (and (> choice 0) (< choice 6))
        (cond 
          ((= choice 1) (print "1 chosen"))
          ((= choice 2) (print "2 chosen"))
          ((= choice 3) (print "3 chosen"))
          ((= choice 4) (print "4 chosen"))
          ((= choice 5) (print "5 chosen"))
          (t (print "invalid entry, exiting."))))
            choice))

报告的错误非常普遍:

*** - LOOP: illegal syntax near
       (COND ((= CHOICE 1) (PRINT "1 chosen")) ((= CHOICE 2) (PRINT "2 chosen")) ((= CHOICE 3) (PRINT "3 chosen"))
        ((= CHOICE 4) (PRINT "4 chosen")) ((= CHOICE 5) (PRINT "5 chosen")) (T (PRINT "0 chosen, exiting.")))
      in
       (LOOP FOR CHOICE = (OR (PARSE-INTEGER (PROMPT-READ "Choice: ") :JUNK-ALLOWED T) 0) WHILE (AND (> CHOICE 0) (< CHOICE 6))
        (COND ((= CHOICE 1) (PRINT "1 chosen")) ((= CHOICE 2) (PRINT "2 chosen")) ((= CHOICE 3) (PRINT "3 chosen"))
         ((= CHOICE 4) (PRINT "4 chosen")) ((= CHOICE 5) (PRINT "5 chosen")) (T (PRINT "0 chosen, exiting."))))

虽然代码中有'do',但错误信息中没有报告。

语法错误消失了。您应该实际尝试您的代码。

我不明白为什么你没有正确缩进代码。如果没有适当的缩进,您将无法编写任何工作代码,尤其是无法工作的 Lisp 代码。

您的代码:

(defun getchoice3 ()
  (let ( (choice 1) )
    (format t  "~%Enter a number (1-5): ")
    (loop for choice = (or (parse-integer (prompt-read "Choice: ") :junk-allowed t) 0)
      while (and (> choice 0) (< choice 6)) do
        (cond 
          ((= choice 1) (print "1 chosen"))
          ((= choice 2) (print "2 chosen"))
          ((= choice 3) (print "3 chosen"))
          ((= choice 4) (print "4 chosen"))
          ((= choice 5) (print "5 chosen"))
          (t (print "invalid entry, exiting."))))
        choice))   ; <- WHY THIS INDENTATION?

正确格式化的代码看起来更像这样(没有单一的格式化方法,但缩进总是相同的):

(defun getchoice3 ()
  (let ((choice 1))
    (format t  "~%Enter a number (1-5): ")
    (loop for choice = (or (parse-integer (prompt-read "Choice: ")
                                          :junk-allowed t)
                           0)
          while (and (> choice 0)
                     (< choice 6))
          do (cond 
              ((= choice 1) (print "1 chosen"))
              ((= choice 2) (print "2 chosen"))
              ((= choice 3) (print "3 chosen"))
              ((= choice 4) (print "4 chosen"))
              ((= choice 5) (print "5 chosen"))
              (t (print "invalid entry, exiting."))))
    choice))

例如,您看到最后一行的区别了吗?我的版本缩进正确。

为什么这很重要?它可能会帮助您理解您的功能将始终 return 1。独立于任何输入,该函数将 return 始终 1。在一定范围内,缩进可以帮助您理解什么属于什么。

缩进不正确。

(let ((a 1))
  (loop for a from 1 to 10)
        a)   ; <-  where does this a belong to???
             ; this indentation indicates that A belongs to the LOOP
             ; which it doesn't

正确的缩进是:

(let ((a 1))
  (loop for a from 1 to 10)
  a)     ; here it's clear to see that A was introduced by the LET construct

所以,不要按照您认为合理的方式缩进代码。 使用编辑器命令正确执行此操作。然后您可以更好地发现代码中的问题。

Lisp 代码可以以任意方式进行格式化和缩进,因为它使用了一种独立于缩进的数据结构:s 表达式。

Lisp 不关心:

(+ a b c)

(+
a               b
        c)

        (+
a
       b

c)

Lisp 都一样。

但不适用于人类。以上只有一个版本可供人类阅读。

如果您不努力缩进和格式化您的代码,为什么有人要努力回答您的问题,到目前为止,这些问题都是由琐碎的语法错误引起的。