string - 如何在 lisp 中输出不带引号且不返回任何内容的字符串?

标签 string return output lisp common-lisp

我是 lisp 编程的新手,我想知道如何像大多数语言一样输出不带引号且不返回对象(包括不返回 nil)的字符串?

std::cout<<"Hello world\n";

我知道 format t 函数执行此操作,但它仍然返回 nil 没有 nil 和引号的输出方式吗?可能吗?

谁能给我指点 Lisp 教程,比如 thisthis但有更详细的文档和解释?

最佳答案

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/

上一篇:lispworks 和 sbcl

下一篇:Lisp:TAGBODY 问题

相关文章:

c++ - 更改 vector 中对象的私有(private)变量

c - 函数返回错误值

java - 无法将 "java"命令的输出重定向到文本文件

testing - CMake:为 ctest 设置环境变量(或以其他方式自动从 ctest/make test 获取失败的测试输出)

string - 如何仅删除前导和尾随空格,同时使用 excel 公式在单词之间单独留下空格?

c++ - 在C++中获取字符串的长度

Java 字符串缓冲区 - 如何删除

c++ - 如何从函数返回 char 数组?

input - Go - 如何知道输出 channel 何时完成

c - 为什么在c中使用scanf()读取限制时无法访问第一个字符串元素