clisp - 如何在lisp中进行循环?
问题描述
刚开始学习和编码 lisp,我正在尝试创建一个程序,该程序将持续接受一个数字,并且仅当且仅当最后一个输入数字是前一个数字的两倍时才停止。
这是我的代码
----------
(let((a 0)
(b 0)
(count 0))
(loop
(= a b))
(princ"Enter Number: ")
(defvar a(read))
(format t "~% a = ~d" a)
(setq count (1+ count))
(while
(!= b(* a 2) || <= count 1)
(princ "Program Terminated Normally")
)
谢谢
解决方案
一点反馈
(let ((a 0)
(b 0)
(count 0))
(loop
(= a b)) ; here the LOOP is already over.
; You have a closing parenthesis
; -> you need better formatting
(princ"Enter Number: ")
(defvar a(read))
(format t "~% a = ~d" a)
(setq count (1+ count))
(while
(!= b(* a 2) || <= count 1)
(princ "Program Terminated Normally")
)
一些改进的格式:
(let ((a 0)
(b 0)
(count 0))
(loop
(= a b)) ; LOOP ends here, that's not a good idea
(princ "Enter Number: ")
(defvar a(read)) ; DEFVAR is the wrong construct,
; you want to SETQ an already defined variable
(format t "~% a = ~d" a)
(setq count (1+ count))
(while ; WHILE does not exist as an operator
(!= b(* a 2) || <= count 1) ; This expression is not valid Lisp
(princ "Program Terminated Normally")
)
在真正编写这样的循环之前,您可能需要学习更多的 Lisp 运算符。您可能还想以交互方式使用 Lisp 并尝试一些事情,而不是尝试将代码编写到编辑器中并且永远不会从 Lisp 获得反馈......
推荐阅读
- javascript - Eloquent Javascript (findInRemoteStorage) 。然后执行一个函数,但选择不返回解析为其返回值的承诺
- mysql - 如何正确使用“JSON_TABLE”从 MySQL8 中的 Json 数组中获取匹配的名称?
- animation - FFmpeg多个drawtext过滤器彼此相邻
- python - 从没有零的 3d 数组中取平均值
- javascript - Vimeo iframe 嵌入视频仅对其他 Vimeo iframe 自动暂停。当播放同一页面上的 YouTube iframe 时,它们不会暂停
- ios - 如何在 Objective-C 中将多个 ViewController 链接到 NavigationController
- amazon-web-services - AWS:帮助在 Elastic Beanstalk 上配置端到端加密
- javascript - React axios - TypeError:无法读取未定义的属性'post'
- javascript - 使用 AJAX、JQuery 和 HTML 显示/隐藏数据
- java - 如何用 java 2d 数组解决这个问题?