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

带循环和条件的Lisp代码中的语法错误,lisp,common-lisp,Lisp,Common 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) do while (and (> choice 0) (< choice 6)) (c

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

(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?
正确的缩进是:

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

Lisp也是如此

但不适合人类。以上只有一个版本对人类有用


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

为什么不从正确缩进和格式化代码开始呢?这将使查找语法错误变得更容易。这是怎么回事?第二个代码行已经没有缩进,尽管它应该缩进。修好它!带有变量选项的最后一行不能正确缩进。修好它。然后问问你自己:给定循环的语法,为什么DO在错误的位置,应该在哪里?我试图将DO置于while条件之后,但它仍然不起作用。是的,它现在起作用了。谢谢你觉得这种格式怎么样:@RN太可怕了。不要用它。
(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
(+ a b c)
(+
a               b
        c)
        (+
a
       b

c)