Test whether a list contains a specific value in Clojure

后端 未结 18 2253
自闭症患者
自闭症患者 2020-11-30 17:17

What is the best way to test whether a list contains a given value in Clojure?

In particular, the behaviour of contains? is currently confusing me:

18条回答
  •  一生所求
    2020-11-30 18:04

    The problem with the 'recommended' solution is it is breaks when the value you are seeking is 'nil'. I prefer this solution:

    (defn member?
      "I'm still amazed that Clojure does not provide a simple member function.
       Returns true if `item` is a member of `series`, else nil."
      [item series]
      (and (some #(= item %) series) true))
    

提交回复
热议问题