在通用Lisp代码中插入长的多字符串变量或常量的惯用方式是什么?在unix或其他语言中是否存在类似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")
我们该怎么写。如果有任何方法,当你不需要转义引号,它会更好。
发布于 2014-10-09 05:22:29
我不懂习语,但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"
发布于 2014-10-09 04:06:25
没有这回事。
压痕通常是:
(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
"
需要更多的抛光..。你可以使用‘串-修剪’或类似的。
发布于 2014-10-09 18:45:25
我有时会用这个表格:
(concatenate 'string "Blah blah blah"
"Blah blah blah"
"Blah blah blah"
"Blah blah blah")
https://stackoverflow.com/questions/26277184
复制相似问题