Logo

Programming-Idioms

History of Idiom 49 > diff from v31 to v32

Edit summary for version 32 by _benjamin:
New Scheme implementation by user [benjamin]

Version 31

2017-10-21, 18:37:04

Version 32

2018-12-01, 16:58:26

Idiom #49 Split a space-separated string

Build list chunks consisting in substrings of input string s, separated by one or more space characters.

Illustration

Idiom #49 Split a space-separated string

Build list chunks consisting in substrings of input string s, separated by one or more space characters.

Illustration
Code
(define (tokenize l)
  (let loop ((t '())
             (l l))
    (if (pair? l)
        (let ((c (car l)))
          (if (char=? c #\space)
              (cons (reverse t) (loop '() (cdr l)))
              (loop (cons (car l) t) (cdr l))))
        (if (null? t)
            '()
            (list (reverse t))))))

(define (string-split s)
  (map list->string (tokenize (string->list s))))
Origin
https://stackoverflow.com/questions/47028798/scheme-convert-a-string-to-a-list-without-charachters-and-with-spaces