LISP - 将列表写入文件
LISP - write a list to file
我需要将一个数字列表写入文件,并在行尾添加一个 return
。
我试过使用此代码,但仅适用于列表的第一个元素。
(defun write-segment (filename segment)
(cond ((null segment)
(with-open-file (out filename
:direction :output
:if-exists :append
:if-does-not-exist :create)
(format out "~%")))
(T (with-open-file (out filename
:direction :output
:if-exists :append
:if-does-not-exist :create)
(format out "~D " (first segment))
(write-segment filename (cdr segment))))))
有人可以帮我解决这个问题吗?
像这样将 % 附加到流中怎么样:
(with-open-file (str "filename.txt"
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(format str "~A~%" '(1 2 3 4 5)))
在你的情况下,我会做一些事情,比如遍历列表并写入流,像这样的事情,小心额外的 return,你也可以在打开文件之前添加一个控件,如果如果列表为空,你不想做任何事情。
(defun write-non-empty-list-to-a-file (file-name lst)
"writes a non empty list to a file if the list is empty creates the file with a return"
(with-open-file (str file-name
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(dolist (e lst)
(format str "~A~%" e))
(format str "~%")));this if you want an extra return
根据描述和代码,我不能 100% 确定以下内容是否符合您的要求,但无论如何我都会尝试。此代码段中的代码遍历数值列表并将它们写入输出文件:
(defun write-numeric-list(filename l)
(with-open-file (out filename :direction :output :if-exists :append :if-does-not-exist :create)
(dolist (segment l)
(format out "~D " segment))
(format out "~%")))
调用示例:
(write-numeric-list "output.txt" (list 1 2 -42))
此代码只为整个列表打开一次输出文件,而不是像原始版本那样为列表的每个元素打开一次。您可能需要根据特定情况的先决条件调整 :if-exists
和 :if-does-not-exist
选项。
事实上,format
可以使用稍微高级的格式控制字符串自行遍历列表。这些控制字符串不是每个人都喜欢的,但作为参考,这里是使用它们的代码版本:
(defun write-numeric-list(filename l)
(with-open-file (out filename :direction :output :if-exists :append :if-does-not-exist :create)
(format out "~{~D ~}~%" l)))
我需要将一个数字列表写入文件,并在行尾添加一个 return
。
我试过使用此代码,但仅适用于列表的第一个元素。
(defun write-segment (filename segment)
(cond ((null segment)
(with-open-file (out filename
:direction :output
:if-exists :append
:if-does-not-exist :create)
(format out "~%")))
(T (with-open-file (out filename
:direction :output
:if-exists :append
:if-does-not-exist :create)
(format out "~D " (first segment))
(write-segment filename (cdr segment))))))
有人可以帮我解决这个问题吗?
像这样将 % 附加到流中怎么样:
(with-open-file (str "filename.txt"
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(format str "~A~%" '(1 2 3 4 5)))
在你的情况下,我会做一些事情,比如遍历列表并写入流,像这样的事情,小心额外的 return,你也可以在打开文件之前添加一个控件,如果如果列表为空,你不想做任何事情。
(defun write-non-empty-list-to-a-file (file-name lst)
"writes a non empty list to a file if the list is empty creates the file with a return"
(with-open-file (str file-name
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(dolist (e lst)
(format str "~A~%" e))
(format str "~%")));this if you want an extra return
根据描述和代码,我不能 100% 确定以下内容是否符合您的要求,但无论如何我都会尝试。此代码段中的代码遍历数值列表并将它们写入输出文件:
(defun write-numeric-list(filename l)
(with-open-file (out filename :direction :output :if-exists :append :if-does-not-exist :create)
(dolist (segment l)
(format out "~D " segment))
(format out "~%")))
调用示例:
(write-numeric-list "output.txt" (list 1 2 -42))
此代码只为整个列表打开一次输出文件,而不是像原始版本那样为列表的每个元素打开一次。您可能需要根据特定情况的先决条件调整 :if-exists
和 :if-does-not-exist
选项。
事实上,format
可以使用稍微高级的格式控制字符串自行遍历列表。这些控制字符串不是每个人都喜欢的,但作为参考,这里是使用它们的代码版本:
(defun write-numeric-list(filename l)
(with-open-file (out filename :direction :output :if-exists :append :if-does-not-exist :create)
(format out "~{~D ~}~%" l)))