2

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.

fpt
  • 358
  • 3
  • 11

2 Answers2

5
(intern (format nil "~a~a" "FOO" 42) "WHAT-EVER-PACKAGE-YOU-WANT")
Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
  • 1
    Rainer, this would be perfect except that the symbols have "|" prepended and appended to them: |foo42| instead of foo42, which is problematic for my application. Is there an easy way to get foo42 instead of |foo42|? – fpt Oct 22 '13 at 17:11
  • 2
    Those are just to tell the reader not to upcase the printed representation upon reading. – Svante Oct 22 '13 at 17:34
  • So, the symbol with the name "foo42" is represented as `|foo42|`, and the symbol with the name "FOO42" can be represented as `foo42`, `FOO42`, `Foo42`, `fOO42` or whatever. – Svante Oct 22 '13 at 17:35
  • Thank you, Rainer and Svante! This answer-thread's solution let me concatenate my symbols without going through strings. – fpt Oct 22 '13 at 18:10
2

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.

Community
  • 1
  • 1
fpt
  • 358
  • 3
  • 11