lisp - 使用 Lisp 在循环中检查偶数和奇数
问题描述
我不明白为什么下面的 lisp 程序显示 15 行输出而不是 10 行:
(defparameter x 1)
(dotimes (x 10)
(if (oddp x)
(format t "x is odd~%"))
(format t "x is even~%"))
我在 Windows 10 机器上使用 CLISP 2.49。
解决方案
除了公认的答案,请注意使用自动缩进编辑器(例如使用 Emacs)可以轻松发现这些错误。您的代码自动缩进如下:
(dotimes (x 10)
(if (oddp x)
(format t "x is odd~%"))
(format t "x is even~%"))
if
和第二个表达式format
垂直对齐(它们是树中的兄弟姐妹dotimes
),而您希望第二个format
仅在测试失败时发生,与第一个具有相同的深度。
评论
您还可以考虑一些代码:
(format t
(if (oddp x)
"x is odd~%"
"x is even~%"))
甚至:
(format t
"x is ~:[even~;odd~]~%"
(oddp x))
以上依赖于条件格式。
推荐阅读
- reference - 我可以将局部变量的所有权和对它们的引用转移给返回的迭代器吗?
- download - 你如何下载彭博盘中数据
- bash - 有没有办法组合两个 bash 命令的输出?
- android - 如何从cordova项目中删除android依赖项
- windows - 如何在 windows (7,10) 中使用 bat 文件启动 jupyter notebook 服务器?
- android - Xamarin Android 问题通过 HTTPS 连接到具有自签名证书的站点:“未找到证书路径的信任锚。”
- swift - URLSessionDataTask 忽略 URLRequest 的 httpMethod 属性
- wordpress - 在 Gutenberg 的 Image 块中上传 SVG 图像
- pytorch - Pyro:使用 SVI 的简单逆图形示例不起作用
- vue.js - Vue JS Sweetalert 确认刷新模块