Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I append a number to a string in Racket?

Python : xx = "p" + "y" + str(3) => xx == "py3"
How can I get the same result using Racket?

(string-append "racket" (number->string 5) " ")  

Is there another way in Racket, similar to the Python example above, to append a number to a string?

like image 297
book-life Avatar asked Nov 24 '11 14:11

book-life


2 Answers

$ racket
Welcome to Racket v5.3.5.
-> (~a "abc" "def")
"abcdef"
-> (~a "abc" 'xyz 7 )
"abcxyz7"
-> 
like image 109
alinsoar Avatar answered Sep 26 '22 02:09

alinsoar


Python automatically coerces the number to a string, while Racket will not do so. Neither Racket nor Python will coerce the number into a string. That is why you must use number->string explicitly in Racket, and str() in Python ("p" + str(3)). You may also find Racket's format function to behave similarly to some uses of Python's % operator:

# Python
"py %d %f" % (3, 2.2)

;; Racket
(format "rkt ~a ~a" 3 2.2)

But there is no Racket nor Python equivalent to "foo" + 3 that I know of.

[Answer edited per my mistake. I was confusing Python behavior with JavaScript, misled by OP]

like image 33
Dan Burton Avatar answered Sep 24 '22 02:09

Dan Burton