Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 313
fpt Avatar asked Oct 26 '25 03:10

fpt


2 Answers

(intern (format nil "~a~a" "FOO" 42) "WHAT-EVER-PACKAGE-YOU-WANT")
like image 161
Rainer Joswig Avatar answered Oct 28 '25 03:10

Rainer Joswig


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.

like image 45
fpt Avatar answered Oct 28 '25 02:10

fpt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!