bcheung@cs.concordia.ca (CHEUNG yik-chi) (05/25/91)
Hi, Can any one show me a way to do the following function. How can I transfer a string like "abc ( 1 2 3) def" to (abc (1 2 3) def) thanks Benjamin Y.C Cheung.
maverick@cork.Berkeley.EDU (Vance Maverick) (05/25/91)
<cl> (defun string-to-list (string) (with-input-from-string (strm (concatenate 'string "(" string ")")) (read strm))) STRING-TO-LIST <cl> (string-to-list "abc ( 1 2 3) def") (ABC (1 2 3) DEF) <cl>
john@thelonius.mitre.org (John D. Burger) (05/25/91)
bcheung@cs.concordia.ca (CHEUNG yik-chi) writes:
How can I transfer a string like "abc ( 1 2 3) def" to
(abc (1 2 3) def)
Here's an extremely egregious version:
(defun cheesy-string-to-list (string)
(values (read-from-string (concatenate 'string "(" string ")"))))
And here's one that's not a memory hog:
(defun string-to-list (string)
(with-input-from-string (stream string)
(let ((eof stream)
(result '()))
(loop
(let ((object (read stream nil eof)))
(if (eq object eof)
(return (nreverse result))
(push object result)))))))
--
John Burger john@mitre.org
"You ever think about .signature files? I mean, do we really need them?"
- alt.andy.rooney
gat@forsight.jpl.nasa.gov (Erann Gat) (05/25/91)
(defun string->list (s) (read-from-string (concatenate 'string "(" s ")")))
weinrich@clarity.Princeton.EDU (Tim Weinrich) (05/29/91)
From: maverick@cork.Berkeley.EDU (Vance Maverick) <cl> (defun string-to-list (string) (with-input-from-string (strm (concatenate 'string "(" string ")")) (read strm))) STRING-TO-LIST <cl> (string-to-list "abc ( 1 2 3) def") (ABC (1 2 3) DEF) <cl> I strongly recommend putting an ERRSET (or your dialect's equivalent) around that READ for most applications. Otherwise, a very good answer. Twinerik