Given a string containing delimited tokens, return a list of the tokens in string form.
The string to split.
A list of characters that should be treated as whitespace.
(define (split str #!optional (whitespace '(#\space))) ;; Splits string at whitespace and returns the resulting list of tokens (let loop ((characters (string->list str)) ; Top-level recursive loop. (current-word '()) (tokens '())) ; If there are no characters left, ; then we're done! (cond ((null? characters) ; Is there a token in progress? (if (null? current-word) (reverse tokens) (reverse (cons (list->string (reverse current-word)) tokens)))) ; If there are characters left, ; then keep going. (#t (let ((c (car characters)) (rest (cdr characters))) ; Are we reading a space? (cond ((member c whitespace) (if (null? current-word) (loop rest '() tokens) (loop rest '() (cons (list->string (reverse current-word)) tokens)))) ; We are reading a non-space (#t (loop rest (cons c current-word) tokens))))))))