Skip to content

Instantly share code, notes, and snippets.

@Yoxem
Created April 30, 2016 22:55
Show Gist options
  • Save Yoxem/ecfff94614501530703252c7cf077568 to your computer and use it in GitHub Desktop.
Save Yoxem/ecfff94614501530703252c7cf077568 to your computer and use it in GitHub Desktop.
;; split a string with a token-list
;; eg.
;; (string-split "dccdd53cccdd" '(#\c #\d))-> ("d" "c" "c" "d" "d" "53" "c" "c" "c" "d" "d" "")
;; (string-split "demo, char., detecting " '(#\c #\d)) -> ("d" "emo, " "c" "har., " "d" "ete" "c" "ting ")
(define (string-split string token-list)
(define temp-string "")
(string-split-iter token-list string temp-string '()))
(define (string-split-iter token-list rest-string temp-string result-list)
(define rest-str-len (string-length rest-string))
(define 1st-char (if (> (string-length rest-string) 0) (string-ref rest-string 0) ""))
(cond ((eq? rest-str-len 0)
(append result-list (list temp-string)))
((memq 1st-char token-list)
(string-split-iter
token-list
(substring rest-string 1 rest-str-len)
""
(if (eq? (string-length temp-string) 0) (append result-list (list (string 1st-char)))
(append result-list (list temp-string (string 1st-char))
))))
(else
(string-split-iter
token-list
(substring rest-string 1 rest-str-len)
(string-append temp-string (string 1st-char))
result-list)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment