Lisp 格式程序

发布于 2024-10-04 02:09:57 字数 527 浏览 0 评论 0原文

我开始用 Lisp 编程,但在使用 Format 函数时遇到了困难。

我的目标是将整数子列表列表打印为行的 N 个整数。例如:

'((1 2 3)
(4 5 6)
(7 8 9))

应打印为

1 2 3
4 5 6
7 8 9

我尝试在格式化过程中使用迭代,但失败了。

我写的是:

(format t "~{~S ~}" list)

但是这样我得到的子列表是“(1 2 3)”而不是“1 2 3”,所以我尝试了:

(format t "~:{ ~S ~}" list)

这次我进入了子列表,但只打印了第一个元素,所以我介入了并将该函数重写为:

(format t "~:{ ~S ~S ~S ~}" list)

它适用于具有 3 个元素的子列表,但如何使其适用于 n 个元素?

谢谢!

Im starting to program in Lisp and having an hard time with the Format function.

My objective is to print a list of integer sublists as N integers for line. For example:

'((1 2 3)
(4 5 6)
(7 8 9))

should be printed as

1 2 3
4 5 6
7 8 9

I tried using iteration in the format procedure, but I failed.

What I wrote was:

(format t "~{~S ~}" list)

But with this I get the sublists as "(1 2 3)" instead of "1 2 3", so i tried:

(format t "~:{ ~S ~}" list)

this time I got into the sublists but only printed the first element, so I stepped in and re-wrote the function to:

(format t "~:{ ~S ~S ~S ~}" list)

It works for sublists with 3 elements, but how can I make it work for n elements?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

素年丶 2024-10-11 02:09:57
(format t "~{~%~{~A~^ ~}~}"  '((1 2 3) (4 5 6) (7 8 9)))

印刷

1 2 3
4 5 6
7 8 9
(format t "~{~%~{~A~^ ~}~}"  '((1 2 3) (4 5 6) (7 8 9)))

prints

1 2 3
4 5 6
7 8 9
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文