我熟悉从文件中收集 Lisp 对象的基本模板,如下所示:
(with-open-file (stream "filename.lisp")
(loop for object = (read stream nil 'eof)
until (eq object 'eof)
collect object))
但我不确定如何将其转换为从字符串中收集对象,例如使用read-from-string
。那么您是否必须跟踪字符串中停止位置的索引?另外,如何避免与输入中的 eof
或任何其他合法 Lisp 对象(例如 nil
或 t
)发生名称冲突?
最佳答案
您可以使用with-input-from-string
从字符串中读取。
为了防止与 eof
符号发生冲突,您可以使用未驻留的符号或动态创建的其他对象。
(with-input-from-string (stream "this is a (list of 3 things)")
(loop with eof-marker = '#:eof
for object = (read stream nil eof-marker)
until (eq object eof-marker)
collect object))
关于string - 从字符串中读取 Lisp 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42867749/