Is it possible to determine the length of a list in F #?

For example, I define an entry as:

type a = {b : float; c: int list}

but I already know that this list should have a predefined size, say 2, and if the list is not 2, it will be a different type or error, since it is not defined by that type.

Is it possible to determine the size of the list, as it happens in other languages ​​that you must determine the size? Depending on the application, this question may be applied to an array.

+3
source share
1 answer

Perhaps you should use an array instead of a list, since the array has a fixed length:

// create an array of length = 2, initialized with zeros.
let cValues : int[] = Array.create 2 0
cValues.IsFixedSize // returns true

: , . ( ) , fst snd. , . , , , , , F #. , . , .

let fourTuple = (5, 10, 2, 3)
let _,_,third,_ = fourTuple
printfn "%d" third // displays "2"

, , , :

type ListOfC = {c1 : int; c2 : int}
type a' = {b' : float; c' : ListOfC}

, , script . , , , . .

type ListOfTwo(firstInt : int, secondInt : int) =
    member this.First = firstInt
    member this.Second = secondInt

let myListOfTwo = ListOfTwo(4, 5)
myListOfTwo.First

type a = {b : float; c : ListOfTwo }
+3

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


All Articles