数字在Clojure中落在间隔内吗? [英] Does number fall in interval in Clojure?

查看:60
本文介绍了数字在Clojure中落在间隔内吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有比以下更好的方法:

 (defn in-interval?
返回谓词测试其参数是否在包含区间[a,b]的
中。
[ab]
(fn [x](and(> = xa)(< = xb ))))

使用中:

 ((间隔?5 8)5.5);真
((间隔5 8)9); false

我不想使用 range

解决方案


有比这更好的方法了吗?以下内容:


 (<= 5 8 8.5)

它可以与任意数量的参数一起使用,并检查它们是否有序。有3个参数,这就是您要寻找的。

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.

解决方案

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.

这篇关于数字在Clojure中落在间隔内吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆