Why Clojure idiom prefer to return nil instead of empty list like Scheme?

前端 未结 4 708
粉色の甜心
粉色の甜心 2021-01-01 21:27

From a comment on another question, someone is saying that Clojure idiom prefers to return nil rather than an empty list like in Scheme. Why is that?

Like,



        
4条回答
  •  忘掉有多难
    2021-01-01 21:48

    From The Joy of Clojure

    Because empty collections act like true in Boolean contexts, you need an idiom for testing whether there's anything in a collection to process. Thankfully, Clojure provides such a technique:

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

    In other Lisps, like Common Lisp, the empty list is used to mean nil. This is known as nil punning and is only viable when the empty list is falsey. Returning nil here is clojure's way of reintroducing nil punning.

提交回复
热议问题