prevent terminal output in LISP

情到浓时终转凉″ 提交于 2019-12-11 01:59:40

问题


I want to run a function but not have it output the result in the terminal. For example, (set 'A 'B) normally returns B in the console like the following:

 >>> (set 'A 'B)
 B
 >>> A
 B

I don't want it to return anything; I still want the function to do what it's supposed to, just silently:

 >>> (set 'A 'B)
 >>> A
 B

回答1:


It's not perfect, but you can use (values) at the end of your expression to suppress output. You get a blank line instead.

Common Lisp:

(progn (set 'A 'B) (values))

I'm not sure of the equivalent in Scheme.

A lisp REPL always prints some return value. If you really didn't want output, you could run your code as a script in the terminal.

Example:

#!/path/to/interpreter
(set 'A 'B)
[rest of program]



回答2:


Since the value printed is actually a return value of your function, and the return value of a function is the value of last expression evaluated, you can simply add an "empty" (returning e.g. "") instruction at the end of/after your call.




回答3:


I came to the same solution as user1613254, however I made a macro for this (have it in my .sbclrc):

(defmacro m-ignore (fun &body body)
"ignores the return value of a function"
  `(progn (,fun ,@body) 
  (values)))

You use it like this:

(m-ignore format t "text")

The output would be:
text

instead of:
text
NIL

which would be printed when using

(format t "text")


来源:https://stackoverflow.com/questions/16369474/prevent-terminal-output-in-lisp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!