在Common Lisp代码中插入长多字符变量或常量的惯用方法是什么? 在unix shell或其他语言中是否有类似HEREDOC的东西,以消除字符串文字中的缩进空格?
例如:
(defconstant +help-message+
"Blah blah blah blah blah
blah blah blah blah blah
some more more text here")
; ^^^^^^^^^^^ this spaces will appear - not good
写这种方式有点难看:
(defconstant +help-message+
"Blah blah blah blah blah
blah blah blah blah blah
some more more text here")
我们应该如何写它。如果有任何办法,当你不需要逃避报价时,它会更好。
答案 0 :(得分:4)
我不知道惯用语,但format
可以为你做这件事。 (当然。format
可以做任何事情。)
参见Hyperspec第22.3.9.3节,Tilde换行符。未修饰,它删除换行符和后续空格。如果要保留换行符,请使用@
修饰符:
(defconstant +help-message+
(format nil "Blah blah blah blah blah~@
blah blah blah blah blah~@
some more more text here"))
CL-USER> +help-message+
"Blah blah blah blah blah
blah blah blah blah blah
some more more text here"
答案 1 :(得分:2)
没有这样的事情。
缩进通常是:
(defconstant +help-message+
"Blah blah blah blah blah
blah blah blah blah blah
some more more text here")
也许使用阅读器宏或阅读时间评估。草图:
(defun remove-3-chars (string)
(with-output-to-string (o)
(with-input-from-string (s string)
(write-line (read-line s nil nil) o)
(loop for line = (read-line s nil nil)
while line
do (write-line (subseq line 3) o)))))
(defconstant +help-message+
#.(remove-3-chars
"Blah blah blah blah blah
blah blah blah blah blah
some more more text here"))
CL-USER 18 > +help-message+
"Blah blah blah blah blah
blah blah blah blah blah
some more more text here
"
需要更多抛光......你可以使用' string-trim'或类似的。
答案 2 :(得分:1)
我有时会使用这种形式:
(concatenate 'string "Blah blah blah"
"Blah blah blah"
"Blah blah blah"
"Blah blah blah")