F-Sharp (F #) untyped infinity

I wonder why F-Sharp does not support infinity.

This will work in Ruby (but not in f #):

let numbers n = [1 .. 1/0] |> Seq.take(n)

-> System.DivideByZeroException: Attempted division by zero.

I can write the same functionality in a very complicated way:

let numbers n = 1 |> Seq.unfold (fun i -> Some (i, i + 1)) |> Seq.take(n)

-> works

However, I think the first one would be much clearer. I cannot find an easy way to use dynamically typed infinity in F #. There is an infinity keyword, but it is equal to float:

let a = Math.bigint +infinity;;

System.OverflowException: BigInteger cannot represent infinity. in System.Numerics.BigInteger..ctor (double value) at. $ FSI_0045.main @ () stopped due to an error


Edit: this also works on iteration:

let numbers n = Seq.initInfinite (fun i -> i+1) |> Seq.take(n)
+3
2

, F # ( , Ruby ), .

-, Int32. MaxValue. Double .

, :

let numbers n = seq { 1. .. 1./0. } |> Seq.take(n)

, Seq.initInfinite - . . (, , Double.PositiveInfinity 1./0.)

, , haskell: seq {1..} , seq, , .

: , Seq.initInfinite.

+8

, F # : inline, , " ", ( int32, int64, bigint,... , +, ):

let inline infiniteRange start skip = 
    seq {
        let n = ref start
        while true do
            yield n.contents
            n.contents <- n.contents + skip
    }

//val inline infiniteRange :
//   ^a ->  ^b -> seq< ^a>
//    when ( ^a or  ^b) : (static member ( + ) :  ^a *  ^b ->  ^a)
+3

Source: https://habr.com/ru/post/1719958/


All Articles