aut*_*ton 4 common-lisp string-formatting
(format)
在Common lisp中是否有重复指令,类似于(我知道这不起作用):
(format t "~5C" #\*)
Run Code Online (Sandbox Code Playgroud)
只是想知道是否没有比这更优雅的方法:(来自rosettacode )
(defun repeat-string (n string)
(with-output-to-string (stream)
(loop repeat n do (write-string string stream))))
(princ (repeat-string 5 "hi"))
Run Code Online (Sandbox Code Playgroud)
(defun write-repeated-string (n string stream)
(loop repeat n do (write-string string stream)))
(write-repeated-string 5 "hi" *standard-output*))
Run Code Online (Sandbox Code Playgroud)
通常,您可以使用格式迭代:
(format t "~v@{~A~:*~}" 5 "hi")
Run Code Online (Sandbox Code Playgroud)
~A
可以输出各种项目,而不仅仅是字符.有关更多信息,请参阅uselpa的链接答案.
上面从第一个参数获取迭代次数.因此v
,波浪形的背后.
其余参数将由迭代消耗.因此@
.
在迭代中,我们返回一个元素.因此~:*
.
它类似于(format t "~v{~A~:*~}" 5 '("hi"))
,可能更容易理解.