4

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))

and if I execute the function, it prints out "x = 5". But I don't want to printout in a buffer, because if I have a large amount of iteration, the speed of simulation will be decreased.

Any idea?

2
  • The question is unclear and (format t ...) doesn't look valid, is that elisp or Clojure or...? Commented Nov 2, 2011 at 1:00
  • 3
    (format t ...) is Common Lisp, not elisp. I don't think this really has anything to do with Emacs. Commented Nov 2, 2011 at 1:13

3 Answers 3

8

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.
Sign up to request clarification or add additional context in comments.

Comments

2

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

However having excessive disk I/O will also will increase your running time, hence you can consider having two modes like a debug and a release mode for your program where the debug mode prints all the diagnostic messages and the release mode does not print anything at all.

4 Comments

Thank you for your comments. But what I really want is, like a C program, you can run a program like foo > a.txt, if so the output is printed in a.txt file. I am wondering is there any way to do this in lisp.
After you compile your code you still can redirect your executable's standard output using a.txt. Another way would be to call the interpreter like clisp >a.txt . That way all the REPL and your standard output will be redirected to a.txt
loudandclear, I don't quite understand your comment. Can you explain in more detail?
If you compile your code and create an executable, called myexecutable, then you can redirect the output to a file by calling myexecutable >a.txt. If you don't want compilation, but want to send your output to a file, then either use Matthias Benkard's solution or start your interpreter (it is clisp in this example) as: clisp >a.txt and everything including the REPL will be written to a.txt If you don't want to write everything including REPL, only format expressions then you can call format as: (format error-output "something") and invoke your interpreter as "clisp 2>a.txt"
1

I'm not sure I understand your question, but the second argument to format is a 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 would allow you to select 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.