当你输入“你好,世界"时在 Clojure REPL 中,为什么它说“nil"? [英] When you type "hello, world" in Clojure REPL, why does it say 'nil'?

查看:18
本文介绍了当你输入“你好,世界"时在 Clojure REPL 中,为什么它说“nil"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将其输入到 Clojure REPL(使用 enclojure Netbeans 插件):

I typed this into Clojure REPL (using the enclojure Netbeans plugin):

user=> "hello, world"
"hello, world"
nil

什么是零?

推荐答案

Clojure 中的每个函数或宏调用都返回一个值,甚至是诸如 if 语句或循环结构或顶级函数定义或打印语句之类的东西, 在其他语言中是语句".Lisp 中没有语句/表达式二分法;一切都是一种表达.

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 和朋友们打印到标准输出作为副作用并返回 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 打印到标准输出的内容.nilprintln 的返回值.在第二种情况下,"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 接口将有助于将标准输出(上面的 hello world 行)与您在 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.

这篇关于当你输入“你好,世界"时在 Clojure REPL 中,为什么它说“nil"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆