format - 帮助打印表格
这个问题可能会以捂脸结束,但我已经尝试了一段时间,尽管阅读了超规范,但仍然卡住了。
基本上我想做的是类似的事情
(format t "~{|~{ ~5d~}|~%~}" '((1 23 2 312) (23 456 1 7890)))
,但不应该对 5 进行硬编码,而是应该从列表中计算(任何嵌套列表中最长元素的长度 + 1),以给出类似的内容,
| 1 23 2 312|
| 23 456 1 7890|
也许我在这里想得太复杂了,有一种更简单的方法可以做我想做的事,但我想我让自己陷入了无法摆脱的心理困境。
This question will probably end in a facepalm, but I've tried for a while and am still stuck despite reading through the hyperspec.
Basically what I want to do is something like
(format t "~{|~{ ~5d~}|~%~}" '((1 23 2 312) (23 456 1 7890)))
but instead of hard-coding the 5 it should be calculated from the list (length of longest element from any nested list + 1) to give something like
| 1 23 2 312|
| 23 456 1 7890|
Maybe I'm thinking way too complicated here and there is an easier way to do what I want, but I think I ran myself into a mental corner that I can't get out of.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你有两个选择:让
format
魔法消失并使用其他循环结构或生成格式字符串本身:longest-member
的定义留作练习给读者。I think that you have two options: let the
format
magic go and use other looping constructs or generate the format string itself:The definition of
longest-member
is left as an exercise to the reader.假设需要的宽度与width
绑定,那么可以这样做:5
已被替换为V 和
width
已作为参数添加到FORMAT
/编辑:原始答案未正确解释嵌套指令
在格式控制字符串中,可以使用
V
来代替任何常量值,指示相应的值将从参数列表中获取。您可以尝试以下操作:
此格式字符串要求每个值前面都有所需的宽度。
(mapcar ...)
表达式可以完成此任务。Assuming the required width is bound towidth
, then you can do this:5
has been replaced byV
andwidth
has been added as an argument toFORMAT
/edit: original answer did not correctly account for the nested directives
In a format control string
V
may be used in place of any constant value, indicating that the corresponding value is to be taken from the argument list instead.You can try this:
This format string requires the desired width to precede each value. The
(mapcar ...)
expression accomplishes this.