converting number to string in lisp

自闭症网瘾萝莉.ら 提交于 2019-12-03 05:47:11

问题


I tried to find a lisp function to convert between numbers and strings and after a little googling I fond a function with the same name. when I entered (itoa 1) SLIME printed:

Undefined function ITOA called with arguments (1) .

How can I do the conversion?


回答1:


From number to string:

(write-to-string 5)
"5"

you may transform a string to any numerical notation:

(write-to-string 341 :base 10)
"341"

From string to number:

(parse-integer "5")
5

with some trash

(parse-integer " 5 something not a number" :junk-allowed t)
5

Or use this:

(read-from-string "23 absd")
23



回答2:


A heavyweight solution is to use FORMAT:

[2]> (format nil "~A" 1)
"1"

There is also WRITE-TO-STRING:

[3]> (write-to-string 10)
"10"



回答3:


FYI: I believe (itoa #) is only a function in AutoLISP - the LISP variant embedded in AutoCAD drafting software. AutoLISP has far fewer functions than Common Lisp and sometimes identical functions with a different name or functions with the same name that operate differently.

That's probably why it didn't work for you. I use AutoLISP regularly and (itoa #) would do exactly what you want there.



来源:https://stackoverflow.com/questions/16220607/converting-number-to-string-in-lisp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!