I was working on Markov chains yesterday. I tried to read a file as a list of strings, which proved surprisingly difficult. There doesn't seem to be a function analogous to C++'s stream insertion operator, though there is a read-line. I could just do (cl-ppcre:split (read-line stream)) and append the lines together. In fact...
Here's my attempt at read-word anyway.
(with-open-file (in txt)
(labels ((rec ()
(unless (equal 'end-of-file
(peek-char t in nil 'end-of-file))
(append
(cl-ppcre:split "\ " (read-line in))
(rec)))))
(rec)))
...It took all of about five minutes and worked perfectly on the first try, parsing a rather large book in 0.25 seconds. Don't I feel like an ass.Here's my attempt at read-word anyway.
(defun read-word (stream)
(let ((c (peek-char nil stream nil)))
(if (or (char= c #\Space)
(char= c #\Newline))
(string (read-char stream))
(concatenate 'string
(string (read-char stream))
(read-word stream)))))
This is surely reinventing the wheel, poorly, and preserves whitespace in the returned string. Would anyone care to suggest how I can get the correct behavior without complicating the code? Or the name of a library that already has read-word?"If you want to improve, be content to be thought foolish and stupid." -Epictetus