clisp中atoi对应的标准函数是什么?

发布于 2024-12-05 17:07:29 字数 583 浏览 0 评论 0原文

在 Visual Lisp 中,您可以使用 (atoi "123")"123" 转换为 123。 clisp 中似乎没有类似“atoi”的功能?

任何建议表示赞赏!


现在我想将 '(1 2 3 20 30) 转换为 "1 2 3 20 30",那么最好的方法是什么?

parse-interger 可以将字符串转换为整数,如何将整数转换为字符串?我需要使用 format 功能吗?

(map 'list #'(lambda (x) (format nil "~D" x)) '(1 2 3)) => ("1" "2" "3")

但我不知道如何像 haskell 那样将其转换为 "1 2 3"

concat $ intersperse " " ["1","2","3","4","5"] => "1 2 3 4 5"

真诚的!

In visual lisp, you can use (atoi "123") to convert "123" to 123. It seems there is no "atoi" like function in clisp ?

any suggestion is appreciated !


Now i want to convert '(1 2 3 20 30) to "1 2 3 20 30", then what's the best way to do it ?

parse-interger can convert string to integer, and how to convert integer to string ? Do i need to use format function ?

(map 'list #'(lambda (x) (format nil "~D" x)) '(1 2 3)) => ("1" "2" "3")

But i donot know how to cnovert it to "1 2 3" as haskell does:

concat $ intersperse " " ["1","2","3","4","5"] => "1 2 3 4 5"

Sincerely!

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

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

发布评论

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

评论(1

帅气尐潴 2024-12-12 17:07:29

在 Common Lisp 中,您可以使用 read-from-string< /code>用于此目的的函数:

> (read-from-string "123")
123 ;
3

如您所见,主要返回值是读取的对象,在本例中恰好是一个整数。第二个值 - 位置 - 更难解释,但这里它指示字符串中的下一个可能的字符,在后续调用使用相同字符的读取函数时需要读取该字符输入。

请注意,read-from-string 显然不仅仅适合读取整数。为此,您可以转向 parse-integer 函数。它的接口类似于 read-from-string:

> (parse-integer "123")
123 ;
3

假设您要求与 atoi 类似,那么 parse-integer 函数就是更合适的选择。


解决问题的第二部分,即后期编辑,您可以将字符串与 格式函数。此示例使用 < code>format 迭代控制指令 ~{(开始)、~}(结束)和 ~^ (如果剩余则终止输入为空):

> (format nil "Interleaved: ~{~S~^ ~}." '(1 2 3))
"Interleaved: 1 2 3."

粗略翻译,格式字符串表示,

对于输入列表中的每个项目 (~{),通过正常转换 (~S) 打印该项目。如果没有剩余项目,则停止迭代 (~^)。否则,打印一个空格,然后对下一项重复该过程 (~})。

如果您想避免对那里的单个空格进行硬编码,并接受分隔符字符串作为单独提供的值,有几种方法可以做到这一点。目前尚不清楚您是否需要那么大的灵活性。

In Common Lisp, you can use the read-from-string function for this purpose:

> (read-from-string "123")
123 ;
3

As you can see, the primary return value is the object read, which in this case happens to be an integer. The second value—the position—is harder to explain, but here it indicates the next would-be character in the string that would need to be read next on a subsequent call to a reading function consuming the same input.

Note that read-from-string is obviously not tailored just for reading integers. For that, you can turn to the parse-integer function. Its interface is similar to read-from-string:

> (parse-integer "123")
123 ;
3

Given that you were asking for an analogue to atoi, the parse-integer function is the more appropriate choice.


Addressing the second part of your question, post-editing, you can interleave (or "intersperse") a string with the format function. This example hard-codes a single space character as the separating string, using the format iteration control directives ~{ (start), ~} (end), and ~^ (terminate if remaining input is empty):

> (format nil "Interleaved: ~{~S~^ ~}." '(1 2 3))
"Interleaved: 1 2 3."

Loosely translated, the format string says,

For each item in the input list (~{), print the item by its normal conversion (~S). If no items remain, stop the iteration (~^). Otherwise, print a space, and then repeat the process with the next item (~}).

If you want to avoid hard-coding the single space there, and accept the separator string as a separately-supplied value, there are a few ways to do that. It's not clear whether you require that much flexibility here.

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