jak*_*ter 1 lisp string return common-lisp output
我是lisp编程的新手,我想知道如何在没有引号的情况下输出字符串而不返回nil
像大多数语言一样的对象(包括不返回a )?
std::cout<<"Hello world\n";
Run Code Online (Sandbox Code Playgroud)
我知道format t
函数会这样做,但它仍然返回nil
没有一种方法输出没有nil和引号?可能吗?
Rai*_*wig 11
REPL打印它执行的每个表达式的值
如果使用READ EVAL PRINT LOOP,REPL将打印结果.这就是它被称为Read Eval Print Loop的原因.
但输出函数本身不会打印出结果.
CL-USER 1 > (format t "hello")
hello ; <- printed by FORMAT
NIL ; <- printed by the REPL
CL-USER 2 > (format t "world")
world ; <- printed by FORMAT
NIL ; <- printed by the REPL
Run Code Online (Sandbox Code Playgroud)
现在结合以上内容:
CL-USER 3 > (defun foo ()
(format t "hello")
(format t "world"))
FOO
CL-USER 4 > (foo)
helloworld ; <- printed by two FORMAT statements
; as you can see the FORMAT statement did not print a value
NIL ; <- the return value of (foo) printed by the REPL
Run Code Online (Sandbox Code Playgroud)
如果不返回任何值,则REPL将不打印任何值.
CL-USER 5 > (defun foo ()
(format t "hello")
(format t "world")
(values))
FOO
CL-USER 6 > (foo)
helloworld ; <- printed by two FORMAT statements
; <- no return value -> nothing printed by the REPL
Run Code Online (Sandbox Code Playgroud)
您可以在没有REPL的情况下执行Lisp代码
如果在没有REPL的情况下使用Lisp,则无论如何都不会打印任何值:
$ sbcl --noinform --eval '(format t "hello world~%")' --eval '(quit)'
hello world
$
Run Code Online (Sandbox Code Playgroud)
或者你可以让Lisp执行一个Lisp文件:
$ cat foo.lisp
(format t "helloworld~%")
$ sbcl --script foo.lisp
helloworld
$
Run Code Online (Sandbox Code Playgroud)
实际的命令行是特定于实现的.