List to string conversion in Racket -


how convert list string in drracket? example, how convert '(red yellow blue green) "red yellow blue green"? tried using list->string seems work characters.

the trick here mapping on list of symbols received input, converting each 1 in turn string, taking care of adding white space in-between each 1 except last. 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 simpler, using higher-order procedures:

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

either way, works expected:

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

and thorough, if input list list of strings (not symbols in question), answer be:

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

Comments