简体   繁体   中英

Lisp: Convert Number to String

What's a good Common Lisp function to convert a number into a string?

I wish to convert a number to string, as in: 42 -> "42"

Ultimately I want to concatenate a string and a set of numbers together into a set of symbols, like:

(loop for i upto 3
 collect (concatenate 'string "foo" (some-conversion-function i)) into stngs
 finally (return (mapcar #'read-from-strings stngs)))

-> foo0 foo1 foo2 foo3

All numbers are integers.

I've got everything working using (read-from-string (concatenate 'string …)) except that I'm missing a function that'll convert the number into a string or other sequence that'll concatenate into a string.

Alternatively, of course it'd be great if I could skip the strings altogether and just concatenate a symbol and a number into a symbol, as in: foo 0 -> foo0 …if someone could name a Common Lisp function that'd concatenate symbols directly.

(intern (format nil "~a~a" "FOO" 42) "WHAT-EVER-PACKAGE-YOU-WANT")

Grr, despite all my searching of Stack Overflow for various combinations of "lisp" "number" and "string" before I posted my question I didn't find write-to-string or similar until Stack Overflow decided to show me what's "Related" to my question. >|O

Anyway, write-to-strings works nicely for converting numbers to strings: converting number to string in lisp

But it'd still be better to concatenate symbols and numbers directly to symbols.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM