我如何在 common lisp 中格式化 alist?

发布于 2024-08-02 02:03:40 字数 574 浏览 5 评论 0原文

我开始写一些 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 技术交流群。

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

发布评论

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

评论(5

Oo萌小芽oO 2024-08-09 02:03:40

Freenode 上的 #cl-gardeners 频道​​建议执行解构循环绑定,如下所示:

(loop for (a . b) in *mapping*
  do (format t "~a: ~a" a b))

The #cl-gardeners channel on Freenode suggests doing a destructuring loop bind like this:

(loop for (a . b) in *mapping*
  do (format t "~a: ~a" a b))
对你再特殊 2024-08-09 02:03:40

你是对的,因为看起来没有任何方法可以从 FORMAT 中分离出 cons 单元格。

如果您定义另一个函数来格式化单个关联:

(defun print-assoc (stream arg colonp atsignp)
  (format stream "~A: ~A" (car arg) (cdr arg)))

那么很简单:

(format t "~{~/print-assoc/~%~}" *map*)

我不确定这是否是一种改进。 一方面,它有点复杂,但另一方面,它确实将 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:

(defun print-assoc (stream arg colonp atsignp)
  (format stream "~A: ~A" (car arg) (cdr arg)))

then it's easy:

(format t "~{~/print-assoc/~%~}" *map*)

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.

地狱即天堂 2024-08-09 02:03:40

我认为这里的要点是不要对你的列表使用虚线列表。 当然,你节省了一个 cons 单元格,但你放弃了所有好的序列和列表功能。 这根本不值得。 对于完全形成的列表,您的格式化示例很简单:

(defvar *map* '((0 "zero") (1 "one") (2 "two")))
(format t "~:{~a: ~a~}" *map*)

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:

(defvar *map* '((0 "zero") (1 "one") (2 "two")))
(format t "~:{~a: ~a~}" *map*)
饮惑 2024-08-09 02:03:40

我认为没有更好的方法可以做到这一点; 我会使用map()

(format t "~{~a~%~}"
  (map 'list
    #'(lambda (entry)
      (format nil "~a: ~a" (car entry) (cdr entry))
    *map*))

I don't think there's a better way to do it; I would used map():

(format t "~{~a~%~}"
  (map 'list
    #'(lambda (entry)
      (format nil "~a: ~a" (car entry) (cdr entry))
    *map*))
永不分离 2024-08-09 02:03:40

使用将 alist 单元格 (a . 2) 转换为列表 (a 2)

(mapcar #'(lambda (x) `(,(car x) ,(cdr x))) *map*)

,然后按格式进行处理。

例如,要将 ((a . 2) (b . 3)) 打印为 "a=2&b=3"

使用

(format t "~{~{~a~^=~}~^&~}" (mapcar #'(lambda (x) `(,(car x) ,(cdr x))) *map*))

Convert the alist cells (a . 2) to list (a 2) using

(mapcar #'(lambda (x) `(,(car x) ,(cdr x))) *map*)

and then process in format.

For eg., to print ((a . 2) (b . 3)) as "a=2&b=3"

use

(format t "~{~{~a~^=~}~^&~}" (mapcar #'(lambda (x) `(,(car x) ,(cdr x))) *map*))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文