Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn a list of string into one string in scheme?

For example I have (list "a" "1" "b" "2" "c" "3").

Now I want to turn this list into one "a1b2c3".

How do I do that?

Thank you.

like image 833
user2444642 Avatar asked Jun 02 '13 07:06

user2444642


3 Answers

For what it's worth, here are some implementations with and without a delimiter (i.e. a string that is inserted between each pair of strings, such as a space or a comma).

The functions fold and fold-right are from SRFI 1.

Using a string port is probably faster when concatenating very many or very long strings. Otherwise there's unlikely to be much speed difference.

Without delimiter argument

Using fold

(define (string-join strings)
  (fold-right string-append "" strings))

Using recursion

(define (string-join strings)
  (let loop ((strings strings) (so-far ""))
    (if (null? strings)
        so-far
        (loop (cdr strings) (string-append so-far (car strings))))))

Using a string port

(define (string-join strings)
  (parameterize ((current-output-port (open-output-string)))
    (for-each write-string strings)
    (get-output-string (current-output-port))))

With a delimiter argument

Using fold

(define (string-join strings delimiter)
  (if (null? strings)
      ""
      (fold (lambda (s so-far) (string-append so-far delimiter s))
            (car strings)
            (cdr strings))))

Using recursion

(define (string-join strings delimiter)
  (if (null? strings)
      ""
      (let loop ((strings (cdr strings)) (so-far (car strings)))
        (if (null? strings)
            so-far
            (loop (cdr strings)
                  (string-append so-far delimiter (car strings)))))))

Using a string port

(define (string-join strings delimiter)
  (if (null? strings)
      ""
      (parameterize ((current-output-port (open-output-string)))
        (write-string (car strings))
        (for-each (lambda (s)
                    (write-string delimiter)
                    (write-string s))
                  (cdr strings))
        (get-output-string (current-output-port)))))
like image 131
Lassi Avatar answered Nov 05 '22 01:11

Lassi


Don't reinvent the wheel! in Racket, there exists one procedure specifically for this and its' called string-join:

(string-join '("a" "1" "b" "2" "c" "3") "")
=> "a1b2c3"

Quoting the documentation:

(string-join strs                
             [sep                
              #:before-first before-first                
              #:before-last before-last              
              #:after-last after-last]) → string?

strs : (listof string?)
sep : string? = " "
before-first : string? = ""
before-last : string? = sep
after-last : string? = ""

Appends the strings in strs, inserting sep between each pair of strings in strs. before-last, before-first, and after-last are analogous to the inputs of add-between: they specify an alternate separator between the last two strings, a prefix string, and a suffix string respectively.

like image 34
Óscar López Avatar answered Nov 05 '22 01:11

Óscar López


(apply string-append (list "a" "1" "b" "2" "c" "3")) or (string-append* "" (list "a" "1" "b" "2" "c" "3")) should work. See: http://docs.racket-lang.org/reference/strings.html

If you wanted a procedure to do this you can just write (define (strings->string sts) (apply string-append sts))

like image 12
Wes Avatar answered Nov 04 '22 23:11

Wes