根据此:http://package.elm-lang.org/packages/elm-lang/core/latest/Basics#isNaN
Elm 支持无穷大并认为它是一个数字。现在我使用 inf = 1/0 作为常量,但我想知道如何导入无穷大,而不是定义它。
那么,Elm 是否有无穷大常数以及如何导入它?
我看到你已经有了答案,但这是使用
Maybe
模拟无穷大的一种方法
infinity =
Nothing
lessThan : Int -> Maybe Int -> Maybe Int
lessThan x y =
case y of
Just y_ ->
if x < y_ then
Just x
else
y
Nothing ->
Just x
如果您要在 Elm 中为
Int
建模无穷大,我建议重复使用 Maybe
,因为接受的答案不清楚,这不是一个很好的选择,而是为这种情况创建一个更明确的自定义类型:
type MaybeInfiniteInt
= Finite Int
| Infinite
min : Int -> MaybeInfiniteInt -> MaybeInfiniteInt
min xInt y =
case y of
Finite yInt ->
if xInt < yInt then
Finite xInt
else
y
Infinte ->
Finite xInt
{-| Note the argument order is designed for reading in a pipe: y |> moreThan x
-}
moreThan : Int -> MaybeInfiniteInt -> MaybeInfiniteInt
moreThan xInt y =
case y of
Finite yInt ->
yInt > xint
Infinte ->
True