clisp中atoi对应的标准函数是什么?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Common Lisp 中,您可以使用
read-from-string< /code>
用于此目的的函数:
如您所见,主要返回值是读取的对象,在本例中恰好是一个整数。第二个值 - 位置 - 更难解释,但这里它指示字符串中的下一个可能的字符,在后续调用使用相同字符的读取函数时需要读取该字符输入。
请注意,
read-from-string
显然不仅仅适合读取整数。为此,您可以转向parse-integer
函数。它的接口类似于 read-from-string:假设您要求与 atoi 类似,那么 parse-integer 函数就是更合适的选择。
解决问题的第二部分,即后期编辑,您可以将字符串与
格式
函数。此示例使用 < code>format 迭代控制指令~{
(开始)、~}
(结束)和~^
(如果剩余则终止输入为空):粗略翻译,格式字符串表示,
如果您想避免对那里的单个空格进行硬编码,并接受分隔符字符串作为单独提供的值,有几种方法可以做到这一点。目前尚不清楚您是否需要那么大的灵活性。
In Common Lisp, you can use the
read-from-string
function for this purpose: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 theparse-integer
function. Its interface is similar toread-from-string
:Given that you were asking for an analogue to
atoi
, theparse-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 theformat
iteration control directives~{
(start),~}
(end), and~^
(terminate if remaining input is empty):Loosely translated, the format string says,
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.