In Haskell, is there infinity :: Num a => a?

2020-02-10 01:13发布

I'm trying to implement a data structure where if I had the use of infinity for numerical comparison purposes, it would simply things greatly. Note this isn't maxBound/minBound, because a value can be <= maxbound, but all values would be < infinity.

No hope?

8条回答
放荡不羁爱自由
2楼-- · 2020-02-10 01:46

If your use case is that you have boundary conditions that sometimes need to be checked, but sometimes not, you can solve it like this:

type Bound a = Maybe a

withinBounds :: (Num a, Ord a) => Bound a -> Bound a -> a -> Bool
withinBounds lo hi v = maybe True (<=v) lo && maybe True (v<=) hi
查看更多
神经病院院长
3楼-- · 2020-02-10 01:58

Maybe you want a Maybe type?

data Infinite a = Infinite | Only a

then write a Num instance for Num a => Infinite a, with the numeric rules you need.

查看更多
登录 后发表回答