I'm not sure why F# does not allow int64
ranges. It sounds like a useful feature... (but I can understand that int
is the standard type for this in C# and perhaps F# tries to follow this pattern).
As for the workarounds, it is worth adding that you can also write inline
higher-order function:
let inline longFor low high f =
let rec loop n =
if n < high then f n; loop (n + 1L)
loop low
...and then you can express for
loops over int64
ranges in a fairly succinct way:
longFor 1L 100L (fun n ->
<whatever> )
I did a couple of experiments and it seems that the F# compiler is able to optimize this fairly decently (the lambda function is inlined and the tail-recursive loop
function is turned into a while
loop). I do not think this is guaranteed so you may need to check this by hand in high-performance code, but it seems to work fine for simpler examples.
There is only one disadvantage - you won't be able to use local mutable variables (let mutable
) because these cannot be captured by a lambda function. So there may be additional cost with indirect ref
cells (but I'm not sure how big problem this is).