Sometimes it is useful to convert text strings to lists. For example, to get substantial input, read-line is most convenient, but it returns a string. If the objective is to parse the input, it is much more convenient to have a list of words than a string.
Here is an example of code to convert a string to a list:
Because of its reliance on read, this function will not work with certain kinds of punctuation. For example:(defun string-to-list (str) (do* ((stringstream (make-string-input-stream str)) (result nil (cons next result)) (next (read stringstream nil 'eos) (read stringstream nil 'eos))) ((equal next 'eos) (reverse result)))) >(string-to-list "this is a string of text") (THIS IS A STRING OF TEXT)
If punctuation is likely to appear in input, then it is necessary to use read-char, which reads one character at a time. It is then possible to inspect each character and process it appropriately if it is problematic. Exactly how to do this will not be covered here as it makes a nice exercise to develop your understanding of LISP input processing.>(string-to-list "Commas cause problems, see") Error: A comma has appeared out of a backquote. Error signalled by READ. Broken at READ. Type :H for Help.
© Colin Allen & Maneesh Dhagat