List to string conversion in Racket

list, racket, scheme, string

Solution

The trick here is mapping over the list of symbols received as input, converting each one in turn to a string, taking care of adding a white space in-between each one except the last. Something like this:

(define (slist->string slst)
  (cond ((empty? slst) "")
        ((empty? (rest slst)) (symbol->string (first slst)))
        (else (string-append (symbol->string (first slst))
                             " "
                             (slist->string (rest slst))))))

Or even simpler, using higher-order procedures:

(define (slist->string slst)
  (string-join (map symbol->string slst) " "))

Either way, it works as expected:

(slist->string '(red yellow blue green))
=> "red yellow blue green"

And just to be thorough, if the input list were a list of strings (not symbols as in the question), the answer would be:

(define strlist (list "red" "yellow" "blue" "green"))
(string-join strlist " ")
=> "red yellow blue green"

Problem

How do I convert a list into a string in DrRacket? For example, how do I convert '(red yellow blue green) into "red yellow blue green"? I tried using list->string but that seems to work only for characters.

Original source