2
votes

Clojure allows for binary functions (every binary function?), in particular, +, to be applied to multiple args:

(+ 1 2 3) ; 6

I understand how it's treated (reduce-like on the list of arguments):

(+ (+ 1 2) 3) => (+ 3 3) => 6

Let's consider relations, say, <, = etc.:

(< 1 2 3) ; true

But now I don't understand how Clojure treats it. It can not be like in the sample above, because (< 1 2) is boolean value and comparison against integer is pointless:

(< 1 2 3) => (< (< 1 2) 3) => (< true 3) ; bad!

It's incorrect. In case of relations, there should be hidden and inside:

(< 1 2 3 4) => (and (< 1 2) (< 2 3) (< 3 4))

This is the questions. How are they treated? I mean, for me it's like there is no uniform treatment for functions (arg list essentially reduced with this function) and comparisons. Does Clojure make distinction between these cases?

2
This is where (source <) comes in handy! - Chiron
@Chiron This is where I'd like to say +10. I use edit-run file loop instead of repl, I haven't get used to this type of dev yet:) - demi
Just to pick a nit, + and < are not binary functions in clojure, + is a function of 0 or more arguments, < is a function of 1 or more. - noisesmith

2 Answers

5
votes

Read the documentation

Returns non-nil if nums are in monotonically increasing order, otherwise false.

Likewise for > nums must be in monotonically decreasing order.

If you read the source (linked from the docs), you see it's equivalent to your "hidden and"

([x y & more]
   (if (< x y)
     (if (next more)
       (recur y (first more) (next more))
       (< y (first more)))
     false)))
2
votes

Just read the docs:

"Returns non-nil if nums are in monotonically increasing order, otherwise false."