[英]Common Lisp Concatenate and newline
I am currently writing a LISP program which analyses the CR results in the form of lists like following: ("I" 0 10 0 20) << (word X0 X1 Y0 Y1) 我当前正在编写一个LISP程序,该程序以如下形式对CR结果进行分析:(“ I” 0 10 0 20)<<(字X0 X1 Y0 Y1)
It must build the whole text using positions of words. 它必须使用单词的位置来构建整个文本。 My code has a cluster analyser which finds out cluster layouts like paragraphs with left- alignment or right or even both.
我的代码有一个群集分析器,可以找出群集布局,例如具有左对齐或右对齐甚至双对齐的段落。 A cluster data structure seems like this: ("cluster name" xline y0 y1 '(cluster words))
群集数据结构看起来像这样:(“群集名称” xline y0 y1'(群集词))
How can i add a new line while i am iterating over a list of strings and concatenate them into result string to create a formatted text from these? 在遍历字符串列表并将它们连接到结果字符串以从中创建格式化文本时,如何添加新行? Example:
例:
"Hi,\n
\n
here is my entry\n
\n
Good bye"
My code seems like following: 我的代码如下所示:
(defun print-formatted-text(txt)
(let
((size (array-total-size txt))
(sorted (sort (sort txt #'compare-mix) #'compare-generic2))
(result ""))
(loop for i from 0 to (1- size) do
(let ((el (aref sorted i)))
(if (word? el)
(setf result (concatenate 'string result (first el) " "))
(if (null (nth 7 el))
nil
(progn
(setf result (concatenate 'string result " "))
(dolist (curr (nth 7 el))
(setf result (concatenate 'string result (first curr) " "))))))))
result))
If the current data isn't a word, then it is a paragraph. 如果当前数据不是单词,则为段落。 It means, i need to add a new line before i give the words of paragraph out and after.
这意味着,我需要在添加段落之前和之后添加新行。
Is the usage of concatenate properly here? 在这里正确使用串联吗?
Thank you for your advices. 感谢您的建议。
I can't quite follow your program, but it's much easier to work with string-streams and use format
, write-string
, write-line
, terpri
, and related functions, eg 我不太了解您的程序,但是使用字符串流并使用
format
, write-string
, write-line
, terpri
和相关功能要容易得多,例如
(let ((lines '("Hi," "Here is my entry" "Good bye")))
(with-output-to-string (stream)
(dolist (line lines)
(write-line line stream)
(terpri stream))))
=>
"Hi,
Here is my entry
Good bye
"
A few things about coding style 有关编码风格的几件事
(defun print-formatted-text(txt)
(let
((size (array-total-size txt))
(sorted (sort (sort txt #'compare-mix) #'compare-generic2))
(result ""))
(loop for i from 0 to (1- size) do
(let ((el (aref sorted i)))
(LOOP FOR e1 ACROSS
sorted (LOOP FOR e1 ACROSS
进行排序
(if (word? el)
(setf result (concatenate 'string result (first el) " "))
repeated concatenation of strings is really wasteful. 重复串接字符串确实很浪费。 As Xach notes, STREAMS are the better abstraction.
正如Xach所说,STREAMS是更好的抽象。
(if (null (nth 7 el))
nil
(progn
use: (when (nth 7 e1)
使用:(
(when (nth 7 e1)
(setf result (concatenate 'string result " "))
(dolist (curr (nth 7 el))
(setf result (concatenate 'string result (first curr) " "))))))))
result))
If you are using concatenate on characters or newline, then you need to enclose in parenthesis so that it is a list, like this '(#\\Newline)
. 如果在字符或换行符上使用串联,则需要用括号括起来,以便它是一个列表,例如
'(#\\Newline)
。
where #\\Newline is the newline character. 其中#\\ Newline是换行符。
All characters, such as those lifted from a string, like this (elt "abc" 1)
need to be made into a list, like this (list (elt "abc" 1)
. 必须将所有字符(例如从字符串中提起的字符)(像这样
(elt "abc" 1)
做成一个列表,像这样(list (elt "abc" 1)
。
For example code: 例如代码:
(concatenate 'string "Hi ther" '(#\\e) "." '(#\\Newline) "How " (list #\\a #\\r #\\e) " you!" ) => "Hi there. How are you!"
You can try this 你可以试试这个
(format nil "~%")
for example 例如
(setf result (concatenate 'string result (format nil "~%")))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.