F# generate a sequence/array of dates

后端 未结 2 1873
我寻月下人不归
我寻月下人不归 2021-02-08 22:55

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(\"         


        
2条回答
  •  盖世英雄少女心
    2021-02-08 23:19

    Here's a funky way of generating a list of dates. Note I'm taking no credit for this whatsoever as I got it from someone else.

    open System
    let a = new DateTime(2013,12,1)
    let b = new DateTime(2013,12,5)
    Seq.unfold (fun d -> if d < b then Some(d, d.AddDays(1.0)) else None) a
     |> Seq.toList;;
    

    It returns:

    val it : DateTime list = [01/12/2013 00:00:00; 02/12/2013 00:00:00; 03/12/2013 00:00:00; 04/12/2013 00:00:00]

提交回复
热议问题