2014-10-09 42 views
3

在Common Lisp代码中插入long multistrings vars或常量的习惯用法是什么? 在unix shell或其他语言中是否存在类似HEREDOC的字符串文字内部的空格符?在Lisp代码中使用long multistring常量(或变量)的习惯用法

例如:

(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") 

我们应该怎么写呢。如果有什么办法,当你不需要逃避报价时,它会更好。

回答

4

我不知道地道,但format可以为你做到这一点。 (自然,format可以做任何事情。)

请参阅Hyperspec第22.3.9.3节,Tilde换行符。未修饰,它删除了换行符和后续空白符。如果您希望保留换行符,使用@修改:

(defconstant +help-message+ 
    (format nil "Blah blah blah blah [email protected] 
       blah blah blah blah [email protected] 
       some more more text here")) 

CL-USER> +help-message+ 
"Blah blah blah blah blah 
blah blah blah blah blah 
some more more text here" 
+0

谢谢,这看起来像合理的事情。 – coredump 2014-10-09 14:47:17

+1

@coredump在常量字符串的情况下,您可以使用#。 (如[Rainer的回答](http://stackoverflow.com/a/26278029/1281433))。 – 2014-10-09 15:04:25

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 
" 

需要更多抛光......您可以使用'字符串修剪'或类似的。

+0

一年,我想过做类似的宏,但如果有人编辑会弄坏的东西在其他编辑器中使用不同缩进设置的代码。 – coredump 2014-10-09 12:10:31

1

我有时用这种形式:

(concatenate 'string "Blah blah blah" 
        "Blah blah blah" 
        "Blah blah blah" 
        "Blah blah blah") 
+0

这会给你:“Blah等等等等等等等等,没有换行符。 – coredump 2014-10-10 10:02:01