Need to write a to-string
function that accepts integer and string.
(to-string 3) ; -> "3"
(to-string "hello") ; -> "\"hello\""
(to-string "hel\"lo") ; -> "\"hel\\\"lo\""
I managed to do so with:
(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")
However, the get-output-bytes
reseting is not very readable. What's the idiomatic Racket way of doing it?
~v
works ok with bare#lang racket
, docs.racket-lang.org/search/index.html?q=~v Still, I could useformat
if I want to cut down the dependency. Thanks again. – Geddes