Common Lisp 连接和换行
我目前正在编写一个 LISP 程序,它以列表的形式分析 CR 结果,如下所示: (“I”0 10 0 20) << (单词 X0 X1 Y0 Y1)
它必须使用单词的位置构建整个文本。 我的代码有一个聚类分析器,它可以找出聚类布局,例如左对齐或右对齐甚至两者的段落。集群数据结构如下所示: ("簇名称" xline y0 y1 '(簇词))
当我迭代字符串列表并将它们连接到结果字符串以从中创建格式化文本时,如何添加新行? 示例:
"Hi,\n
\n
here is my entry\n
\n
Good bye"
我的代码如下所示:
(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))
如果当前数据不是单词,那么它是一个段落。这意味着,我需要在给出段落前后的单词之前添加一个新行。
这里 concatenate 的使用正确吗?
谢谢您的建议。
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)
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))
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不太能理解你的程序,但是使用字符串流并使用
format
、write-string
、write-line
更容易>、terpri
以及相关函数,例如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, e.g.关于编码风格的一些事情
(LOOP FOR e1 ACROSS
排序的字符串重复连接确实很浪费。正如 Xach 所说,STREAMS 是更好的抽象。
使用:
(when (nth 7 e1)< /代码>
A few things about coding style
(LOOP FOR e1 ACROSS
sortedrepeated concatenation of strings is really wasteful. As Xach notes, STREAMS are the better abstraction.
use:
(when (nth 7 e1)
如果您在字符或换行符上使用连接,则需要将其括在括号中,以便它是一个列表,如
'(#\Newline)
。其中#\Newline 是换行符。
所有字符,例如从字符串中提取的字符,如
(elt "abc" 1)
都需要放入列表中,如(list (elt "abc" 1)< :
例如代码
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)
.where #\Newline is the newline character.
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)
.For example code:
你可以尝试这个
例如
You can try this
for example