簡體   English   中英

如何在Lisp中輸出不帶引號且不返回任何內容的字符串?

[英]How to output a string in lisp without quotes and without returning anything?

我是Lisp編程的新手,我想知道如何像大多數語言一樣輸出不帶引號且不返回對象(包括不返回nil )的字符串?

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

我知道format t函數可以做到這一點,但是它仍然返回nil沒有沒有nil和引號的輸出方法嗎? 可能嗎?

有人能指出我的Lisp教程像這樣這樣 ,但更詳細的資料和解釋?

REPL打印它執行的每個表達式的值

如果您使用READ EVAL PRINT LOOP,則REPL將打印結果。 這就是為什么它被稱為讀取評估打印循環。

但是輸出函數本身不會打印其結果。

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
$

實際的命令行是特定於實現的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM