简体   繁体   中英

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|]

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 |]

您也可以使用Array.init

let arr = Array.init 5 (fun i -> (i + 1) * 2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM