TGDZCalc by F# (37th)
想到MS在很多年前还推过一个F#,试着用它来写一下TGDZ Calc, 如下
点击查看代码
let GetBaseYear year =
let mutable b=0
if year>0 then b <- 4
else b <- -57
while b>year do
b <- b-60
while b+60<=year do
b <- b+60
b
let YearToTGDZ year=
let mutable y=year
if y>0 then y <- year-4
else y <- year-3
while y<0 do
y<- y + 60
let t= y % 10
let d=y % 12
let tgStr="甲乙丙丁戊已庚辛壬癸"
let dzStr="子丑寅卯辰已午未申酉戌亥"
tgStr.[t..t] + dzStr.[d..d]
let TGDZToYear(baseYear:int, tgdz:string) =
let tgStr="甲乙丙丁戊已庚辛壬癸"
let dzStr="子丑寅卯辰已午未申酉戌亥"
let tg=tgdz.[0..0]
let dz=tgdz.[1..1]
let ti=tgStr.IndexOf(tg)
let di=dzStr.IndexOf(dz)
let mutable m=ti-di
if m<0 then
m <- 12+m
baseYear + m*5 + ti
let years=[1900..2100]
printfn "年份 天干地支 甲子元年 反推年份"
for i in years do
let mutable tgdz=YearToTGDZ i
let mutable bY=GetBaseYear i
let mutable y2=TGDZToYear(bY, tgdz)
printfn "%d %s %d %d" i tgdz bY y2
//在.net 9.0 命令行 dotnet run 及脚本 dotnet fsi tgdzcalc.fsx 下通过 2025-6-2。
坦白说,用完之后我不喜欢这个语言,F# 虽然学习起来不算难,但这种混杂的风格(缩进表示层次又不让用TAB,变量非得加个mutable, 赋值用<-,错误提示也不准确) 让人很不舒服。怪不得现在没什么人用。