2014-10-09 43 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