English 中文(简体)
为什么Clojure idiom宁愿返回Nil,而不是像计划一样的空洞名单?
原标题:Why Clojure idiom prefer to return nil instead of empty list like Scheme?

https://stackoverflow.com/questions/6033006/demonstrate-first-class-Functions-in-this-simple-example/6033207#6033207" 关于另一个问题,有人说,Clojure idiom宁愿返回Nil,而不是像计划那样的空洞清单。 为什么如此?

(when (seq lat) ...)

instead of

  (if (empty? lat)  
     () ...)
最佳回答

我可以考虑几个原因:

请注意,在Clojure,仍有一些职能,确实是一份空洞清单。 例如:

(rest [1])
=> ()

This question on rest vs. next goes into some detail of why this is.....

问题回答

还指出,收集类型和零类的结合是一种独一无二的做法,将一对一对一加一对一无二。 因此,Nil将空洞清单的异构体分类,同时代表虚假或“放任”价值。

甲胺是另一种常见的单一身份代表虚假价值观的语言:0、零、空洞。

http://www.honofclojure.com/“rel=“noretinger”>。 The Joy of Clojure

由于空洞收集像<代码>true在波兰州的情况中那样,你需要一个辅助器,以测试在收集过程中是否有任何东西。 感谢 衣着提供这种技术:

(seq [1 2 3])
;=> (1 2 3)

(seq [])
;=> nil

在其他特许经营中,如通用许可证,则空档号指nil。 这被称为nil punning,只有在空洞清单不实的情况下才可行。 返回<代码>nil 这里是重新引入绝食的 clo。

自2006年以来 我将撰写一份答复。 (kuro的答复提供了所有信息,但可能太大)

  1. First of all I think that more importend things should be in first.
  2. seq is just what everybody uses most of the time but empty? is fine to its just (not (seq lat))
  3. In Clojure () is true, so normaly you want to return something thats false if the sequence is finished.
  4. if you have only one importend branch in your if an the other returnes false/ () or something like that why should you write down that branch. when has only one branch this is spezially good if you want to have sideeffects. You don t have to use do.

See this example:

(if false () (do (println 1) (println 2) (println 3)))

页: 1

(when true (println 1) (println 2) (println 3))

不是这种分散,而是认为它更好读。


P.S.

职能不称为if-not > 更经常的是<代码>(如果(不是真实的)





相关问题
How to improve Clojures error messages

I ve been playing a bit with Clojure and so far is fairly impressed, but one thing that I keep running into is wierd error messages from Clojure. This comes in two forms: Java errors, like null ...

clojure rmi classpath problem

I am trying to use clojure to implement a "plugin" for some vendor supplied software. Here is a little background on the vendor supplied software. It expects me to implement a particular interface ...

Help translating this Java codeblock to Clojure?

I m getting my feet wet with Clojure, and trying to get used to functional programming. I ve been translating various imperative functions from other languages into their Clojure equivalents -- and ...

Is functional Clojure or imperative Groovy more readable?

OK, no cheating now. No, really, take a minute or two and try this out. What does "positions" do? Edit: simplified according to cgrand s suggestion. (defn redux [[current next] flag] [(if flag ...

taking java method names as function arg in clojure

All, I want to create a function that takes a symbol representing a java method and applies it to some object: (user=> (defn f [m] (. "foo" (m))) When I execute this, I get a result much ...

how to efficiently apply a medium-weight function in parallel

I m looking to map a modestly-expensive function onto a large lazy seq in parallel. pmap is great but i m loosing to much to context switching. I think I need to increase the size of the chunk of work ...

热门标签