Search code examples
arraysf#initialization-list

F# array initialization with non-consecutive numbers


Is there a short notation to initialize F# array with multiples of N, where N > 1? For example N = 2:

{|2; 4; 6; 8; 10;|]

Maybe, something analogous to the default N = 1 case:

[|a..b|]

Solution

  • The syntax for that is in the language:

    let a = [|2..2..10|];
    

    The number in the middle is the step between the values. To be even fancier, you can also use sequence expressions for array initialization:

    let b = [| for i in 1 .. 10 -> i * i |]