用(格式)重复字符串/字符

时间:2014-07-15 09:28:47

标签: common-lisp string-formatting

Common lisp中是否有(format)的重复指令,类似于(我知道这不起作用):

(format t "~5C" #\*)

只是想知道是否没有比这更优雅的方式:(来自rosettacode

(defun repeat-string (n string)
  (with-output-to-string (stream)
    (loop repeat n do (write-string string stream))))

(princ (repeat-string 5 "hi"))

1 个答案:

答案 0 :(得分:8)

(defun write-repeated-string (n string stream)
  (loop repeat n do (write-string string stream)))

(write-repeated-string 5 "hi" *standard-output*))

通常你可以使用格式迭代:

(format t "~v@{~A~:*~}" 5 "hi")

~A可以输出各种项目,而不仅仅是字符。有关更多信息,请参阅uselpa的链接答案。

Above从第一个参数获取迭代次数。因此,代字号后面的v

其余参数将由迭代消耗。因此@

在迭代中,我们返回一个元素。因此~:*

它与(format t "~v{~A~:*~}" 5 '("hi"))类似,可能更容易理解。