我是 lisp 编程的新手,我想知道如何像大多数语言一样输出不带引号且不返回对象(包括不返回 nil
)的字符串?
std::cout<<"Hello world\n";
我知道 format t
函数执行此操作,但它仍然返回 nil
没有 nil 和引号的输出方式吗?可能吗?
最佳答案
REPL 打印它执行的每个表达式的值
如果您使用 READ EVAL PRINT 循环,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
现在结合以上内容:
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
如果您没有返回任何值,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
您可以在没有 REPL 的情况下执行 Lisp 代码
如果你在没有 REPL 的情况下使用 Lisp,无论如何都不会打印任何值:
$ sbcl --noinform --eval '(format t "hello world~%")' --eval '(quit)'
hello world
$
或者你可以让 Lisp 执行一个 Lisp 文件:
$ cat foo.lisp
(format t "helloworld~%")
$ sbcl --script foo.lisp
helloworld
$
实际的命令行是特定于实现的。
关于string - 如何在 lisp 中输出不带引号且不返回任何内容的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49465775/