flipflop
flipflop

Reputation: 3

Convert a list of strings to a String in DrRacket

How do I convert a list of strings to a String in DrRacket? For example

(list "c" "o" "k" "e") to "coke"

Upvotes: 0

Views: 1568

Answers (2)

Atharva Shukla
Atharva Shukla

Reputation: 2137

Adding a second answer to illustrate a few more abstract ways to achieve this.

Using foldr would be the natural way to abstract over the recursive solution provided in the first answer.

> (foldr string-append "" (list "c" "o" "k" "e"))
"coke"

You can also use apply since string-append can accept arbitrarily many arguments:

> (apply string-append (list "c" "o" "k" "e"))
"coke"

If you're using one of the student languages, you can use the handy implode:

> (implode (list "c" "o" "k" "e"))
"coke"

Upvotes: 1

Jorge L Martinez Jr
Jorge L Martinez Jr

Reputation: 21

Try (list->string lst) if you're using a list of characters.

Check the docs for list->string here

Otherwise, if you have a list of strings, try a recursive function with string-append.

(define (lst_to_str lst)
  (cond
   [(empty? lst) ""]
   [else (string-append (first lst) (lst_to_str (rest lst)))]))

Upvotes: 2

Related Questions