Common Lisp 连接和换行

发布于 2024-10-16 21:08:38 字数 1094 浏览 8 评论 0原文

我目前正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蒲公英的约定 2024-10-23 21:08:38

我不太能理解你的程序,但是使用字符串流并使用 formatwrite-stringwrite-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

"

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.

(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

"
贱贱哒 2024-10-23 21:08:38

关于编码风格的一些事情

(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 排序

            (if (word? el)
                (setf result (concatenate 'string result (first el) " "))

的字符串重复连接确实很浪费。正如 Xach 所说,STREAMS 是更好的抽象。

              (if (null (nth 7 el))
                  nil
                (progn

使用: (when (nth 7 e1)< /代码>

                  (setf result (concatenate 'string result " "))
                  (dolist (curr (nth 7 el))
                    (setf result (concatenate 'string result (first curr) " "))))))))
    result))

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

            (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.

              (if (null (nth 7 el))
                  nil
                (progn

use: (when (nth 7 e1)

                  (setf result (concatenate 'string result " "))
                  (dolist (curr (nth 7 el))
                    (setf result (concatenate 'string result (first curr) " "))))))))
    result))
美胚控场 2024-10-23 21:08:38

如果您在字符或换行符上使用连接,则需要将其括在括号中,以便它是一个列表,如 '(#\Newline)

其中#\Newline 是换行符。

所有字符,例如从字符串中提取的字符,如 (elt "abc" 1) 都需要放入列表中,如 (list (elt "abc" 1)< :

例如代码

(连接'字符串 
   “你好呀) ”。” '(#\Newline) “如何” (list #\a #\r #\e) “你!” )

=> “你好呀。
你好吗!”

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:

(concatenate 'string 
   "Hi ther" '(#\e) "." '(#\Newline) "How " (list #\a #\r #\e) " you!" )

=> "Hi there.
How are you!"
蓝咒 2024-10-23 21:08:38

你可以尝试这个

(format nil "~%")

例如

(setf result (concatenate 'string result (format nil "~%")))

You can try this

(format nil "~%")

for example

(setf result (concatenate 'string result (format nil "~%")))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文