将输出打印到文件或不打印输出?

use*_*748 4 lisp common-lisp

当我在lisp中执行特定函数时,我想保存或忽略输出.我使用Emacs和CCL.例如,

(defun foo (x) (format t "x = ~s~%" x))
Run Code Online (Sandbox Code Playgroud)

如果我执行该函数,它会输出"x = 5".但我不想在缓冲区中打印输出,因为如果我有大量的迭代,模拟的速度将会降低.

任何的想法?

Mat*_*ard 8

您可以通过绑定*standard-output*到流来临时重定向标准输出.例如,没有输出流的广播流将作为输出的黑洞:

(let ((*standard-output* (make-broadcast-stream)))
  (foo 10)
  (foo 20))
;; Does not output anything.
Run Code Online (Sandbox Code Playgroud)

您也可以使用其他绑定结构执行此操作,例如:with-output-to-stringwith-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.
Run Code Online (Sandbox Code Playgroud)