我如何在 common lisp 中格式化 alist?
我开始写一些 Common Lisp,并且刚刚掌握将事物组合在一起并格式化它们的窍门。
假设我有一个列表,如下所示:
(defvar *map* '((0 . "zero") (1 . "one") (2 . "two")))
我如何将其格式化为这样?
0: zero
1: one
2: two
我在想类似 (format t "~{~{~a: ~a~}~%~}" *map*)
的东西,但这会产生错误,因为“零”不是名单上,你不能拿走它的车。
当然,执行 (format t "~{~a~%~}" *map*)
(0 . "zero")
(1 . "one")
(2 . "two")
会像预期的那样打印,但这并不完全是我想要的。 有没有比 (dolist (entry *mapping*) (format t "~a: ~a~%" (car Entry) (cdr Entry)))
更好的方法?
I'm beginning to write me some Common Lisp and am just getting the hang of consing things together and formatting them.
Let's suppose I have an alist, like this:
(defvar *map* '((0 . "zero") (1 . "one") (2 . "two")))
How do I format it like this?
0: zero
1: one
2: two
I was thinking something like (format t "~{~{~a: ~a~}~%~}" *map*)
, but that gives an error because "zero" isn't a list and you can't take the car of it.
Of course, doing (format t "~{~a~%~}" *map*)
prints
(0 . "zero")
(1 . "one")
(2 . "two")
like it's supposed to, but it's not quite what I want. Is there a better way to do this than just (dolist (entry *mapping*) (format t "~a: ~a~%" (car entry) (cdr entry)))
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Freenode 上的 #cl-gardeners 频道建议执行解构循环绑定,如下所示:
The #cl-gardeners channel on Freenode suggests doing a destructuring loop bind like this:
你是对的,因为看起来没有任何方法可以从 FORMAT 中分离出 cons 单元格。
如果您定义另一个函数来格式化单个关联:
那么很简单:
我不确定这是否是一种改进。 一方面,它有点复杂,但另一方面,它确实将 print-assoc 分解为(可重用)函数,这可能很有用。
You're right, in that it doesn't look like there's any way to pick apart a cons cell from FORMAT.
If you define another function to format a single association:
then it's easy:
I'm not sure if this is an improvement or not. On the one hand, it's a little more complex, but on the other hand, it does break out print-assoc into a (reusable) function, which could be useful.
我认为这里的要点是不要对你的列表使用虚线列表。 当然,你节省了一个 cons 单元格,但你放弃了所有好的序列和列表功能。 这根本不值得。 对于完全形成的列表,您的格式化示例很简单:
I think the takeaway lesson here is really not to use dotted lists for your alists. You save one cons cell, sure, but you give up all the nice sequence and list functions. It's just not worth it. Your formatting example is trivial with fully formed lists:
我认为没有更好的方法可以做到这一点; 我会使用
map()
:I don't think there's a better way to do it; I would used
map()
:使用将 alist 单元格
(a . 2)
转换为列表(a 2)
,然后按格式进行处理。
例如,要将
((a . 2) (b . 3))
打印为"a=2&b=3"
使用
Convert the alist cells
(a . 2)
to list(a 2)
usingand then process in format.
For eg., to print
((a . 2) (b . 3))
as"a=2&b=3"
use