【问题标题】:How specify a parameter in a FORMAT control-string on Common Lisp as variable? [duplicate]如何将 Common Lisp 的 FORMAT 控制字符串中的参数指定为变量? [复制]
【发布时间】:2017-07-11 05:52:51
【问题描述】:

我能做什么:

CL-USER> (format nil "~10:<foobar~>")
"    foobar"

但我想将10 作为参数传递:

CL-USER> (format nil "~d:<foobar~>" 10) ;; but throws error
"    foobar"

有没有办法做到这一点?因为我不想那样做:

CL-USER> (format nil (format nil "~~~d:<foobar~~>" 10))
"    foobar"

在 Python 或 C 中,可以使用 * 将变量参数传递给控制字符串,如下所示:

print("%*s" % (10, "foobar"))

我想要 Common Lisp 格式的一样。提前致谢。

【问题讨论】:

    标签: format common-lisp


    【解决方案1】:

    我找到了解决方案,只需使用~v 而不是~d。此处对此进行了描述:

    可以使用 V(或 v)代替指令的前缀参数。在这种情况下,format 将 args 中的参数作为指令的参数。参数应该是整数或字符。如果 V 参数使用的 arg 为 nil,则效果就像参数被省略一样。

    见:22.3 Formatted Output - Common Lisp HyperSpec

    CL-USER > (format nil "~v:<foobar~>" 10)
    "    foobar"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      • 1970-01-01
      • 2023-03-23
      • 2014-07-25
      相关资源
      最近更新 更多