Print output into a file or not print output?

后端 未结 3 1498
臣服心动
臣服心动 2021-01-13 13:51

I\'d like to save or ignore outputs when I execute a specific function in lisp. I use Emacs and CCL. For example,

(defun foo (x) (format t \"x = ~s~%\" x))
         


        
3条回答
  •  庸人自扰
    2021-01-13 14:27

    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:

    (let ((*standard-output* (make-broadcast-stream)))
      (foo 10)
      (foo 20))
    ;; Does not output anything.
    

    You can also do this with other binding constructs, such as with-output-to-string or 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.
    

提交回复
热议问题