Skip to content

Instantly share code, notes, and snippets.

@matthewp
Created April 7, 2012 01:43
Show Gist options
  • Save matthewp/2324447 to your computer and use it in GitHub Desktop.
Save matthewp/2324447 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))))
@louis-frayser
Copy link

Thanks. The search engine AI gave me all sorts of implementations that didn't work, and I couldn't fix them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment