scheme - Combining words list to a para in Racket -


i have combine list of words produce para. managed following:

(define (wordlist2para wl)   (define str " ")   (for ((w wl))     (set! str (string-append str w " ")))   (string-trim str))  (wordlist2para '("this" "is" "a" "test")) 

output:

"this test" 

it works not functional. how can write functional code this?

if wanted explicitly , not use string-join, recurse , use 3 cases:

  • the empty list produces empty string
  • a one-element list produces sole element (this avoids having trailing separator)
  • otherwise, append car , space recursion on cdr.

like this:

(define (wordlist2para ws)   (cond ((null? ws) "")         ((null? (cdr ws)) (car ws))         (else (string-append (car ws) " " (wordlist2para (cdr ws)))))) 

Comments