Skip to content

Instantly share code, notes, and snippets.

@cametan001
Created April 30, 2010 20:45
Show Gist options
  • Save cametan001/385725 to your computer and use it in GitHub Desktop.
Save cametan001/385725 to your computer and use it in GitHub Desktop.
CL-USER> (segment-reader t #\/ 3)
あいう/えおか/きくけ/
("あいう" "えおか" "きくけ")
CL-USER>
;;; SEGMENT-READER with DO
(defun segment-reader (stream ch n)
(do ((n n (1- n))
(acc nil
(push
(do ((curr (read-char stream)
(read-char stream))
(chars nil (push curr chars)))
((char= ch curr) (coerce (nreverse chars) 'string)))
acc)))
((< n 1) (nreverse acc))))
;;; SEGMENT-READER
;; Scheme-style
(defun segment-reader (stream ch n &optional (acc nil))
(labels ((iter (chars curr)
(if (char= ch curr)
(coerce (reverse chars) 'string)
(iter (cons curr chars) (read-char stream)))))
(if (< n 1)
(reverse acc)
(segment-reader stream ch (1- n)
(cons (iter nil (read-char stream)) acc)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment