In F# I can easily do
let a = [1 .. 10];;
Then why can’t I do
let a = DateTime.Parse("01/01/2012")
let b = DateTime.Parse("01/01/2020")
let dateList = [a .. b]
It gives an error Type constraint mismatch. The type DateTime is not compatible with type TimeSpan
There are two problems – firstly you need to specify the interval you want to use between elements of the list. This would be a
TimeSpan, however it does not have a staticZeromember.This constraint is required by the skip range operator which requires the ‘step’ type to have static
(+)andZeromembersYou can define your own structure which supports the required operations however:
You can then do:
Edit: Here’s an alternative syntax using discriminated unions that you may prefer: