如何在 Common Lisp 中按顺序创建变量?
我在一个正在读取地图文件的函数内有以下代码。我收到一条错误消息,指出 *numrows*
是非法维度。我认为这是因为 lisp 正在并行处理这些变量。我该如何解决这个问题?
(setq *numrows* (read map))
(setq *numcols* (read map))
(setq *map* (make-array '(*numrows* *numcols*) :initial-element nil))
I have the following code inside a function that is reading in a file which is a map. I get an error that *numrows*
is an illegal dimension. I think this is because lisp is processing these variables in parallel. How can I fix this?
(setq *numrows* (read map))
(setq *numcols* (read map))
(setq *map* (make-array '(*numrows* *numcols*) :initial-element nil))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你误判了问题。您传递给 MAKE-ARRAY 的第一个参数是两个符号的列表:*NUMROWS* 和 *NUMCOLS*。但是,MAKE-ARRAY 的第一个参数应该是非负整数列表。修复示例的最简单方法是使用值创建一个列表:(list *numrows* *numcols*)。因此,代码将如下所示:
不过,您通常不会像这样使用 setq。根据上下文,使用 LET* 绑定这些变量可能会更好:
You're misdiagnosing the problem. The first argument you're passing to MAKE-ARRAY is a list of two symbols, *NUMROWS* and *NUMCOLS*. However, the first argument to MAKE-ARRAY should be a list of non-negative integers. The easiest way to fix your example is to make a list with the values instead: (list *numrows* *numcols*). So the code would look like this instead:
You normally wouldn't use setq like this, though. It'd probably be better, depending on the context, to bind those variables with LET*: