简体   繁体   English

如何在Racket中的字符串中附加数字?

[英]How can I append a number to a string in Racket?

Python : xx = "p" + "y" + str(3) => xx == "py3" Python: xx = "p" + "y" + str(3) => xx == "py3"
How can I get the same result using Racket? 如何使用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? 在Racket中是否有另一种方法,类似于上面的Python示例,将数字附加到字符串中?

Python automatically coerces the number to a string, while Racket will not do so. Python会自动将数字强制转换为字符串,而Racket 则不会这样做。 Neither Racket nor Python will coerce the number into a string. Racket和Python都不会将数字强制转换为字符串。 That is why you must use number->string explicitly in Racket, and str() in Python ( "p" + str(3) ). 这就是为什么你必须在Racket中显式使用number->string在Python中使用str()"p" + str(3) )。 You may also find Racket's format function to behave similarly to some uses of Python's % operator: 您还可以找到Racket的format函数,其行为类似于Python的%运算符的一些用法:

# 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. 但是我所知道的没有Racket 和Python相当于"foo" + 3

[Answer edited per my mistake. [根据我的错误编辑答案。 I was confusing Python behavior with JavaScript, misled by OP] 我把Python行为与JavaScript混淆,被OP误导了]

$ racket
Welcome to Racket v5.3.5.
-> (~a "abc" "def")
"abcdef"
-> (~a "abc" 'xyz 7 )
"abcxyz7"
-> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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