需要编写一个to-string
接受整数和字符串的函数.
(to-string 3) ; -> "3" (to-string "hello") ; -> "\"hello\"" (to-string "hel\"lo") ; -> "\"hel\\\"lo\""
我设法这样做:
(define (to-string x) (define o (open-output-string)) (write x o) (define str (get-output-string o)) (get-output-bytes o #t) str ) (to-string 3) (to-string "hello") (to-string "hel\"lo")
但是,get-output-bytes
重置不是很易读.什么是惯用的球拍方式呢?
~v
功能或~s
功能是否racket/format
适用于您?
> (~v 3) "3" > (~v "hello") "\"hello\"" > (~v "hel\"lo") "\"hel\\\"lo\""