CanadianBeaver
CanadianBeaver

Reputation: 81

In racket how do i combine strings in a list to create one sentence

Essentially I want to combine string values in a list. For example I want (Join(list "cat" "is" "hungry")) to execute ("cat is hungry")

(define(join l) )

Upvotes: 1

Views: 1382

Answers (2)

soegaard
soegaard

Reputation: 31147

Use string-append*

See https://docs.racket-lang.org/reference/strings.html?q=string-append*#%28def._%28%28lib._racket%2Fstring..rkt%29._string-append%2A%29%29

Upvotes: 1

Óscar López
Óscar López

Reputation: 236004

In Racket there's a procedure just for that, it's called string-join:

(string-join '("cat" "is" "hungry"))
=> "cat is hungry"

Here's another alternative, using string-append. It's less efficient, but avoids having to code an explicit recursion:

(define (join lst)
  (foldl (lambda (s acc) (string-append acc " " s))
         (first lst)
         (rest  lst)))

(join '("cat" "is" "hungry"))
=> "cat is hungry"

Upvotes: 1

Related Questions