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

前端 未结 4 672
粉色の甜心
粉色の甜心 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:47

    Since I wrote the comment I will write a answer. (The answer of skuro provides all information but maybe a too much)

    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)))

    you can write

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

    Not that diffrent but i think its better to read.


    P.S.

    Not that there are functions called if-not and when-not they are often better then (if (not true) ...)

提交回复
热议问题