9

What is fundamental difference for having a '?' in every? and not in some functions of clojure?

user> (every? true? [true true false])
false

user> (some true? [true false false])
true

Thanks.

4

2 に答える 2

14

every?trueまたはfalseを返すため、疑問符が付きます。someブール値を返しません。「predによって返された最初の論理的に真の値」を返し、それ以外の場合は返しnilます。

これが私が思いついた不完全な例です:

user=> (some #(if (= 0 %) 1 0)  [1 3 5 0 9])
0 

コレクションの最初の要素が述語に渡され、述語は0と評価されます。これは論理的に真であるため、 0を返します。trueまたはfalseを返さないsomeことがわかります。some

したがってevery?、trueまたはfalseを返すため、疑問符が表示されます。 someまたはnilによって返される値を返すpredため、疑問符は付きません。

于 2012-06-08T17:41:22.777 に答える
0

some doesn't necessarily return a Boolean, whereas every? always does. See the documentation.

Returns the first logical true value of (pred x) for any x in coll, else nil. One common idiom is to use a set as pred, for example this will return :fred if :fred is in the sequence, otherwise nil: (some #{:fred} coll)

于 2012-06-08T17:47:40.653 に答える