当您输入“hello,world”时,在Clojure REPL中,为什么说'nil'? [英] When you type "hello, world" in Clojure REPL, why does it say 'nil'?
问题描述
我在Clojure REPL(使用enclojure Netbeans插件)中输入了这个:
I typed this into Clojure REPL (using the enclojure Netbeans plugin):
user=> "hello, world"
"hello, world"
nil
nil about?
What's the nil about?
推荐答案
每个函数或宏调用都会在Clojure中返回一个值,即使 if
语句或循环结构或toplevel函数定义或print语句,在其他语言中是语句。在Lisps中没有语句/表达式二分法;一切都是一个表达式。
Every function or macro call returns a value in Clojure, even things like if
statements or looping constructs or toplevel function definitions or print statements, which in other languages are "statements". There's no statement/expression dichotomy in Lisps; everything is an expression.
所以 println
和朋友打印到标准输出作为副作用, code> nil ,大多数函数没有任何有用的返回。
So println
and friends print to standard-output as a side-effect and return nil
, as do most functions which don't have anything useful to return.
但是在REPL应返回字符串本身,如 digitalross'帖子中一样。
But typing a literal string at the REPL should return the string itself, as in digitalross' post.
user> (println "hello world")
hello world
nil
user> "hello world"
"hello world"
user>
在第一种情况下, hello world
行是由 println
打印到标准输出的。 nil
是 println
的返回值。在第二种情况下,hello world
是hello world
的返回值,因为字符串自身。在这种情况下没有打印到标准输出。
In the first case, the hello world
line is what was printed to standard-output by println
. nil
is the returned value of println
. In the second case, "hello world"
is the returned value of "hello world"
since a string evaluates to itself. Nothing is printed to standard-output in this case.
(SLIME和一些其他REPL接口将有助于颜色标准输出( / code> line)与您在REPL(
nil
)上输入的值的返回值不同,因为否则可能会混淆。)
(SLIME and some other REPL interfaces will helpfully color standard-output (the hello world
line above) differently from the returned value of what you typed at the REPL (nil
above), since it might be confusing otherwise.)
这是你在REPL应该看到的。您发布的内容必须是Enclojure的工件。
This is what you should see at a REPL. What you posted must be an artifact of Enclojure.
这篇关于当您输入“hello,world”时,在Clojure REPL中,为什么说'nil'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!