From Newsgroup: comp.lang.lisp
Pascal J. Bourguignon wrote:
With a sepacialized splitter:
(defun split-string (string)
(loop
:with in-word := nil
:with result := '()
:with start := 0
:for pos :from 0
:for chr :across string
:do (if in-word
(when (char= #\space chr)
(setf in-word nil)
(push (subseq string start pos) result))
(when (char/= #\space chr)
(setf start pos
in-word t)))
:finally (when in-word
(push (subseq string start pos) result))
(return (nreverse result))))
(defun split-string (text)
(let ((str (substitute #\newline #\space text))
(word t)
result)
(with-input-from-string (s str)
(do () ((not word))
(setf word (read-line s nil nil))
(when (and word (not (equal "" word))) (push word result))))
(reverse result)))
* (split-string " foo bar ")
("foo" "bar")
--
The good news is, it's not Lisp that sucks, but Common Lisp. --- Paul Graham --- Synchronet 3.21a-Linux NewsLink 1.2