gpt4 book ai didi

lisp - 抑制 Lisp 中打印函数的输出

转载 作者:太空宇宙 更新时间:2023-11-03 18:39:12 26 4
gpt4 key购买 nike

我是 Lisp 的新手,遇到了打印问题。我有一个功能可以打印到标准输出(除其他外)。然后我想通过另一个函数运行这个函数,它仍然运行相同但没有打印到标准输出。

这是我的意思的一个简单例子。我描述了以下两个功能:

(defun does-printing()
(print "This goes to standard output."))

(defun run-other-function (function)
(funcall function)
(values))

下面是我运行它时发生的情况,

;; Dribble of #<IO TERMINAL-STREAM> started on 2014-10-05 21:49:49.
#<OUTPUT BUFFERED FILE-STREAM CHARACTER #P"example.out">
[7]> (run-other-function #'does-printing)

"This goes to standard output."

[8]> (dribble)
;; Dribble of #<IO TERMINAL-STREAM> finished on 2014-10-05 21:50:09.

请注意,打印功能仍然打印到标准输出。当通过 run-other-function 运行 does-printing 时,它希望能够以某种方式抑制这种打印。在寻找解决方案时,我尝试了很多不同的问题措辞变体,但没有任何结果是我想做的。

最佳答案

最简单的解决方案是创建一个空的广播流。

(with-open-stream (*standard-output* (make-broadcast-stream))
(call-some-function-1)
...
(call-some-function-n))

如果广播流没有组件流,所有输出都将被丢弃。上面将 *standard-output* 绑定(bind)到这样的流。这不会占用任何数据,而且是可移植的。

关于lisp - 抑制 Lisp 中打印函数的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26209496/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com