如何在 Common Lisp 中按顺序创建变量?

发布于 2024-09-26 02:37:40 字数 258 浏览 1 评论 0原文

我在一个正在读取地图文件的函数内有以下代码。我收到一条错误消息,指出 *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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱格式化 2024-10-03 02:37:40

你误判了问题。您传递给 MAKE-ARRAY 的第一个参数是两个符号的列表:*NUMROWS* 和 *NUMCOLS*。但是,MAKE-ARRAY 的第一个参数应该是非负整数列表。修复示例的最简单方法是使用创建一个列表:(list *numrows* *numcols*)。因此,代码将如下所示:

(setq *numrows* (read map))
(setq *numcols* (read map))
(setq *map* (make-array (list *numrows* *numcols*) :initial-element nil))

不过,您通常不会像这样使用 setq。根据上下文,使用 LET* 绑定这些变量可能会更好:

(let* ((numrows (read map))
       (numcols (read map))
       (map-array (make-array (list numrows numcols) :initial-element nil))
  ; do something with map-array
  )

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:

(setq *numrows* (read map))
(setq *numcols* (read map))
(setq *map* (make-array (list *numrows* *numcols*) :initial-element nil))

You normally wouldn't use setq like this, though. It'd probably be better, depending on the context, to bind those variables with LET*:

(let* ((numrows (read map))
       (numcols (read map))
       (map-array (make-array (list numrows numcols) :initial-element nil))
  ; do something with map-array
  )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文