如何使用 clisp 将字符串转换为列表?

发布于 2024-12-05 02:02:15 字数 94 浏览 1 评论 0原文

如何优雅地将字符串 "1 2 3 4 5 6 7" 转换为列表 (1 2 3 4 5 6 7) ?我正在使用 CLISP。

How can i convert the string "1 2 3 4 5 6 7" into the list (1 2 3 4 5 6 7) elegantly? I am using CLISP.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

雪若未夕 2024-12-12 02:02:15

这是一个递归解决方案。

    ;Turns a string into a stream so it can be read into a list
    (defun string-to-list (str)
        (if (not (streamp str))
           (string-to-list (make-string-input-stream str))
           (if (listen str)
               (cons (read str) (string-to-list str))
               nil)))

Here is a recursive solution.

    ;Turns a string into a stream so it can be read into a list
    (defun string-to-list (str)
        (if (not (streamp str))
           (string-to-list (make-string-input-stream str))
           (if (listen str)
               (cons (read str) (string-to-list str))
               nil)))
小苏打饼 2024-12-12 02:02:15

您应该在 a href="http://www.lispworks.com/documentation/HyperSpec/Body/f_parse_.htm" rel="nofollow">parse-integer 中使用环形。

例如,使用 loop

(let ((string "1 2 3"))
  (loop :for (integer position) := (multiple-value-list 
                                    (parse-integer string
                                                   :start (or position 0)
                                                   :junk-allowed t))
        :while integer
        :collect integer))

(1 2 3)

如果您需要更好地控制拆分,请使用 split-sequencecl-ppcre 库。

如果您需要解析更通用的数字格式,请使用parse-number库。

可以从 Quicklisp 获取库。

You should use parse-integer in a loop.

For example, using loop:

(let ((string "1 2 3"))
  (loop :for (integer position) := (multiple-value-list 
                                    (parse-integer string
                                                   :start (or position 0)
                                                   :junk-allowed t))
        :while integer
        :collect integer))

(1 2 3)

If you need better control about the splitting, use the split-sequence or cl-ppcre library.

If you need to parse more general number formats, use the parse-number library.

Libraries are available from Quicklisp.

雨后彩虹 2024-12-12 02:02:15
(with-input-from-string (s "1 2 3 4 5 6 7" :index i :start 0 :end 13)
              (list (read s) (read s) (read s) (read s) (read s) (read s)))
(1 2 3 4 5 6 7)

它可以工作,但我觉得它不是那么优雅,因为有很多 read 调用。

再次感谢!

(with-input-from-string (s "1 2 3 4 5 6 7" :index i :start 0 :end 13)
              (list (read s) (read s) (read s) (read s) (read s) (read s)))
(1 2 3 4 5 6 7)

it works however i feel it is not so elegant as there are many read call .

thanks again!

醉城メ夜风 2024-12-12 02:02:15

我认为这可能有效:

(setf s "1 2 3 4 5 6 7")
(setf L-temp (coerce s 'list))

这会创建一个以空格作为元素的列表。删除空格:

(setf L-final (remove #\Space L-temp))

I think this might work:

(setf s "1 2 3 4 5 6 7")
(setf L-temp (coerce s 'list))

This makes a list with spaces as elements. Remove spaces:

(setf L-final (remove #\Space L-temp))
梦年海沫深 2024-12-12 02:02:15

我认为斯万特是对的。我之前的尝试没有成功。这是另一种尝试。我使用连接将字符串更改为列表表示形式。然后我使用 read-from-string 将字符串 (s-2) 转换为实际列表。

(setf s-0 "1 2 3 4 5 6 7")
(setf s-1 (concatenate 'string "(" s ")" ))
(setf s-2 (read-from-string s-1))

我将其放入这样的函数中:

(defun from-string-to-list (s)
  (let ((L (read-from-string 
           (concatenate 'string "(" s ")"))))
    L))

“let”和“L”的唯一目的是使函数 from-string-tolist 仅返回列表而不返回多个值。 read-from-string 返回两个值:我认为是列表和字符串的大小。

I see that Svante is right. My previous attempt did not work. Here is another attempt. I use concatenate to change the string into a list representation. Then I use read-from-string to convert the string (s-2) into an actual list.

(setf s-0 "1 2 3 4 5 6 7")
(setf s-1 (concatenate 'string "(" s ")" ))
(setf s-2 (read-from-string s-1))

I roll it into a function like this:

(defun from-string-to-list (s)
  (let ((L (read-from-string 
           (concatenate 'string "(" s ")"))))
    L))

The only purpose of "let" and "L" is to make the function from-string-to-list return only the list and not return multiple values. read-from-string returns two values: The list and the size of the string, I think.

枉心 2024-12-12 02:02:15

这样就可以了,

(with-input-from-string (s "1 2 3 4 5")
   (let ((r nil))
      (do ((line (read s nil 'eof)
                 (read s nil 'eof)))
          ((eql line 'eof))
          (push line r))
   (reverse r)))

That would do,

(with-input-from-string (s "1 2 3 4 5")
   (let ((r nil))
      (do ((line (read s nil 'eof)
                 (read s nil 'eof)))
          ((eql line 'eof))
          (push line r))
   (reverse r)))
迷爱 2024-12-12 02:02:15

您还可以使用 map 函数:

(map 'list #'identity "1 2 3 4 5 6 7")

You can also use the map function:

(map 'list #'identity "1 2 3 4 5 6 7")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文