gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:03:03 26 4
gpt4 key购买 nike

以下代码有什么语法错误?

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

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

为什么这很重要?它可能会帮助您理解您的函数将始终返回 1。独立于任何输入,该函数将始终返回 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 来说都是一样的。

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

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

关于lisp - 带有循环和条件的 Lisp 代码中的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38282826/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com