问题
Is there a better way than the following:
(defn in-interval?
"Returns a predicate that tests if its argument falls in
the inclusive interval [a, b]."
[a b]
(fn [x] (and (>= x a) (<= x b))))
In use:
((in-interval? 5 8) 5.5) ; true
((in-interval? 5 8) 9) ; false
I don't want to use range
, for example, because that constructs a lazy sequence.
回答1:
Is there a better way than the following:
Yes.
(<= 5 8 8.5)
It works with any number of arguments and check if they are ordered. With 3 arguments, it's what you are looking for.
来源:https://stackoverflow.com/questions/17393451/does-number-fall-in-interval-in-clojure