将输出打印到文件中还是不打印输出?
当我在 Lisp 中执行特定函数时,我想保存或忽略输出。我使用 Emacs 和 CCL。例如,
(defun foo (x) (format t "x = ~s~%" x))
如果我执行该函数,它会打印出“x = 5”。但我不想在缓冲区中打印输出,因为如果我有大量迭代,模拟速度就会降低。
有什么想法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过将
*standard-output*
绑定到流来临时重定向标准输出。例如,没有输出流的广播流将充当输出的黑洞:也可以使用其他绑定结构来执行此操作,例如 with-output-to-string 或 with-open-file:
You can temporarily redirect standard output by binding
*standard-output*
to a stream. For example, a broadcast stream with no output streams will serve as a black hole for output:You can also do this with other binding constructs, such as
with-output-to-string
orwith-open-file
:您可以为其提供一个输出文件流,而不是将
t
作为format
的第一个参数,并且该语句的输出将发送到该文件流。然而,过多的磁盘 I/O 也会增加运行时间,因此您可以考虑为程序设置两种模式,例如调试模式和发布模式,其中调试模式打印所有诊断消息,而发布模式则不打印任何内容。全部。
Instead of
t
as the first argument toformat
, you can give it an output file stream and your output for that statement will be sent to that file stream.However having excessive disk I/O will also will increase your running time, hence you can consider having two modes like a debug and a release mode for your program where the debug mode prints all the diagnostic messages and the release mode does not print anything at all.
我不确定我是否理解您的问题,但
format
的第二个参数是 流。如果将其设置为t
,它将打印到标准输出,但您也可以将其设置为打开的文件。所以像这样的东西将允许您选择输出的位置:
I'm not sure I understand your question, but the second argument to
format
is a stream. If you set it tot
it prints to standard output, but you can also set it to an open file.So something like this would allow you to select where the output goes: