Issue with Clojure 'contains'

前端 未结 3 1121
心在旅途
心在旅途 2021-01-11 19:21

I am going through some Clojure tutorials using Closure Box, and entered the following code:

user> (def stooges (vector \"Moe\" \"Larry\" \"Curly\"))
#\'u         


        
3条回答
  •  情书的邮戳
    2021-01-11 20:13

    This is a common trap! I remember falling into this one when I was getting started with Clojure :-)

    contains? checks whether the index (0, 1, 2, etc.) exists in the collection.

    You probably want something like:

    (some #{"Moe"} stooges)
    => "Moe"    
    
    (some #{"Fred"} stooges)
    => nil      
    

    Or you can define your own version, something like:

    (defn contains-value? [element coll]
      (boolean (some #(= element %) coll)))
    
    (contains-value? "Moe" stooges)
    => true
    

提交回复
热议问题