Skip to content

Instantly share code, notes, and snippets.

@vdikan
Forked from matthewp/gist:2324447
Created October 12, 2020 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vdikan/e2e5eee952ce36bc5125a091bc935799 to your computer and use it in GitHub Desktop.
Save vdikan/e2e5eee952ce36bc5125a091bc935799 to your computer and use it in GitHub Desktop.
String Split in Scheme
;;; str-split : Apr 2006 Doug Hoyte, hcsw.org.
;;; ----
;;; Splits a string 'str into a list of strings
;;; that were separated by the delimiter character 'ch
;;; ----
;;; Efficient as possible given that we can't count on
;;; 'str being an immutable string.
(define (str-split str ch)
(let ((len (string-length str)))
(letrec
((split
(lambda (a b)
(cond
((>= b len) (if (= a b) '() (cons (substring str a b) '())))
((char=? ch (string-ref str b)) (if (= a b)
(split (+ 1 a) (+ 1 b))
(cons (substring str a b) (split b b))))
(else (split a (+ 1 b)))))))
(split 0 0))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment