Splits string at every occurance of target and returns the result as a list. Note that match-split returns the occurances of target in the list of tokens.
The string to split.
The string which is a delimiter between tokens
(define (match-split string target)
;; Splits string at target and returns the resulting list of tokens
(if (string? string)
(let loop ((result '()) (current "") (rest string))
(if (< (string-length rest) (string-length target))
(append result (if (equal? (string-append current rest) "")
'()
(list (string-append current rest))))
(if (equal? target (substring rest 0 (string-length target)))
(loop (append result
(if (equal? current "")
'()
(list current))
(list target))
""
(substring rest (string-length target)
(string-length rest)))
(loop result
(string-append current (substring rest 0 1))
(substring rest 1 (string-length rest))))))
(list string)))