首页 \ 问答 \ 带循环和条件的Lisp代码中的语法错误(Syntax error in Lisp code with loop and conditions)

带循环和条件的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”,但错误消息中没有报告。


What is the syntax error in following code?

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

The error being reported is very general:

*** - 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."))))

Though 'do' is there in code, it is not being reported in Error message.


原文:https://stackoverflow.com/questions/38282826
更新时间:2020-03-26 12:00

最满意答案

语法错误消失了。 你应该尝试你的代码。

我不明白为什么你没有正确地缩进代码。 没有适当的缩进,您将无法编写任何工作代码,尤其是不能使用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-expression。

Lisp并不关心:

(+ a b c)

要么

(+
a               b
        c)

要么

        (+
a
       b

c)

这对Lisp来说都是一样的。

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

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


The syntax error is gone. You should actually try your code.

I fail to understand why you are not indenting the code properly. Without proper indentation you will not be able to write any working code, and especially not working Lisp code.

Your code:

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

The code properly formatted looks like more like this (there is not a single one way to format it, but indentation is always the same way):

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

You see the difference for example in the last line? My version is correctly indented.

Why is that important? It may help you to understand that your function will always return 1. Independent of any input, the function will return always 1. Indentation helps you to understand what belongs to what, given some scope.

This is not correctly indented.

(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

The correct indentation is:

(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

So, don't indent the code such a way how you dream it makes sense. Use an editor command to do it correctly. Then you can spot the problems in your code much better.

Lisp code can be formatted and indented in arbitrary ways, because it uses an indentation independent data structure: the s-expression.

Lisp does not care:

(+ a b c)

or

(+
a               b
        c)

or

        (+
a
       b

c)

It's all the same for Lisp.

But not for humans. Only one version of those above is useful for humans to read.

If you don't put any effort into indenting and formatting your code, why should anyone put in the effort to answer your questions, which are so far all caused by trivial syntax errors.

2016-07-09

相关文章

更多

最新问答

更多
  • css在元素之前中断列而不破坏包装器(css break column before element without breaking the wrapper)
  • 如何在Xamarin共享项目中使用自定义渲染器(How to use Custom Renderer in Xamarin Shared Project)
  • 如何为特定表中的特定字段设置唯一?(How to set unique for specific field from specific table?)
  • Google SDK iOS - sign()方法完成处理程序(Google SDK iOS - sign() method completion handler)
  • 在具有接口{}值的地图上实现String()(Implement String() on a map with interface{} values)
  • 检查数据库中是否已存在用户名(Check if username already exist in DB)
  • 使用javascript进行ajax调用时阻止用户交互(Block user interaction while doing ajax call using javascript)
  • 什么'if(err)'在Javascript中精确测试?(What does 'if (err)' tests precisely in Javascript?)
  • jQuery mouseleave无法正常工作(jQuery mouseleave not working)
  • 寻求使用的一些说明(Seeking some clarification on use of )
  • 将数组传递给注释的语法(syntax for passing array to annotation)
  • 用于从两个日期范围之间的文件中提取数据的Shell脚本(Shell script to extract data from file between two date ranges)
  • 元素隐藏但父()没有(Element hides but parent() not)
  • 如何使用Google App Engine Java平台开发web ui(How to develop web ui with Google App Engine Java platform)
  • 对于OWL A级;(For an OWL class A; Getting all properties that A is their domain)
  • Excel VBA公式格式问题(Excel VBA Formula Format Issue)
  • ORA - 02287序列号不允许在这里(ORA - 02287 sequence number not allowed here)
  • Github拉忽略特定文件(Github Pull Ignore Specific File)
  • SQL CONVERT函数在SQL Server中工作但不在应用程序中(SQL CONVERT function working in SQL Server but not in application)
  • backbone.js适用于大型应用程序(backbone.js for large applications)
  • 防止程序关闭(Preventing program from closing)
  • 生成不带图像的heightMap(Generating a heightMap without an Image)
  • Bootstrap - 如何将包含文本的div居中?(Bootstrap - How to center div that has text inside it?)
  • Android - 片段findViewById()总是null?(Android - Fragment findViewById() always null?)
  • 确定CSS中的高度(Figuring out heights in CSS)
  • 使用__autoload包含类和使用命名空间(Use __autoload to include class and use namespace)
  • setTimeout()不允许我传递文本值[重复](setTimeout() doesn't allow me to pass text values [duplicate])
  • 在NSUserDefault中恢复值(Restoring value in NSUserDefault)
  • 知道如何将这种下沉的悬停效果添加到图像/链接吗?(Any idea how to add this sinking hover effect to an image/link?)
  • 在XIB中淡入/淡出UISegmentedControl(fade in/fade out UISegmentedControl in XIB)