4

I am having a tough time trying to find an example of converting a boolean to a string in Scheme.

My problem is that I use string-append to add a few strings together as part of a debugger. My fix was to check if equal to #t, then append "#t", and like-wise with #f.

My question- is there a method in Scheme to convert bools to strings? Something like bool->string?

My Code:

(if (equal? val #t)
                       (string-append (number->string count) ":" "#t")    
                       (string-append (number->string count) ":" "#f")    )

3 Answers 3

4

Use format:

> (format "~a" #t)
"#t"
> (format "~a" #f)
"#f"
Sign up to request clarification or add additional context in comments.

2 Comments

In the latest version of Racket (5.3.1), you can also use the ~a function: (~a #t). You'll need to (require racket/format).
Very nice, I forgot that format already does the implicit string port thing.
4

This might help you:

(define (->string x)
  (call-with-output-string
   (lambda (out)
     (display x out))))

This writes out any object to a string port and returns its string value.

> (->string #t)
"#t"
> (->string #f)
"#f"

Comments

3
(define (boolean-to-string val) (if val "#t" "#f"))

(string-append (number->string count) ":" (boolean-to-string val))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.