Print output to a file or not print output?

I want to save or ignore outputs when executing a specific function in lisp. I use Emacs and CCL. For instance,

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

and if I execute the function, it will output "x = 5". But I do not want to print in the buffer, because if I have a large number of iterations, the simulation speed will be reduced.

Any idea?

+4
source share
3 answers

You can temporarily redirect standard output by binding *standard-output* to the stream. For example, a broadcast stream without 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. 
+7
source

Instead of t you can give it a stream of output files as the first argument to format , and your output for this statement will be sent to this file stream.

However, excessive disk I / O will also increase the runtime, so you can consider using two modes, such as debugging and release mode for your program, where the debugging mode prints all diagnostic messages and the release mode does not work, print anything at all.

+2
source

I'm not sure I understand your question, but the second argument to format is the stream . If you set it to t , it prints to standard output, but you can also set it to an open file.

So, something like this will let you choose where the output goes:

 ;;output to file: (setf *stream* (open "myfile" :direction :output :if-exists :supersede) ;;alternative to output to standard output: ;;(setf *stream* t) (defun foo (x) (format *stream* "x = ~s~%" x)) (foo 10) (close *stream*) ;; only if output sent to a file 
+1
source

Source: https://habr.com/ru/post/1379239/


All Articles