在lisp中执行特定函数时,我希望保存或忽略输出我用Emacs和CCL例如,
(defun foo (x) (format t "x = ~s~%" x))
如果我执行这个函数,它会输出“x=5”但我不想在缓冲区中打印出来,因为如果我有大量的迭代,模拟的速度会降低。
知道吗?
最佳答案
通过将*standard-output*
绑定到流,可以临时重定向标准输出。例如,没有输出流的abroadcast stream将用作输出的黑洞:
(let ((*standard-output* (make-broadcast-stream)))
(foo 10)
(foo 20))
;; Does not output anything.
您还可以使用其他绑定构造来完成此操作,例如
with-output-to-string
或with-open-file
:(with-output-to-string (*standard-output*)
(foo 10)
(foo 20))
;; Does not print anything;
;; returns the output as a string instead.
(with-open-file (*standard-output* "/tmp/foo.txt" :direction :output)
(foo 10)
(foo 20))
;; Does not print anything;
;; writes the output to /tmp/foo.txt instead.