TGDZcalc by Clojure (42th)

尝试了一下函数式编程语言Clojure. 这玩意习惯了也还行,初看确实特别古怪.

点击查看coljure代码
(defn CalcBaseYear[y] 
(def ^:dynamic b 0)
(if (> y 0) (def b 4) (def b -57))
(while (> b y) (def b (- b 60)))
(while (<= (+ b 60) y) (def b (+ b 60)))
b
)
  
(defn YearToTGDZ[year]
 (def strTG "0123456789") 
 (def strDZ "0123456789ABC") 
 (def ^:dynamic Result "") 
 (def ^:dynamic tmpY year)
(if (> tmpY 0) (def tmpY (- tmpY 4)) (def tmpY (- tmpY 3)))
(while (< tmpY 0) (def tmpY (+ tmpY 60)))
(def ti (mod tmpY 10))
(def di (mod tmpY 12))
(def t (subs strTG ti (+ ti 1)))
(def d (subs strDZ di (+ di 1)))
(str t d) 
)

(require '[clojure.string :as string])
(defn TGDZtoYear[tgdz, baseYear] 
(def ^:dynamic Result 0)
(def tg (subs tgdz 0 1))
(def dz (subs tgdz 1 2))
(def strTG "0123456789")
(def strDZ "0123456789ABC")
(def ti (string/index-of strTG tg))
(def di (string/index-of strDZ dz))
(if (>= ti di) (def m (- ti di)) (def m (- (+ ti 12) di)))
(def Result (+ baseYear (* m 5) ti))
Result
)
 
(defn Test[x]
	(def by (CalcBaseYear x))
	(def s (YearToTGDZ x))
	(def CalBack (TGDZtoYear s by))
	(str,  "year ", x,": TGDZ= ",s, "; BaseYear= ",by, "; ReCalcBack= ",CalBack))	

(defn main[]	
	(def ^:dynamic x 0)
	(for [x [-361,1900,1924,1936,1976,2026]] (println (Test x))))

运行结果如下:
(为了在控制台下显示农历的干支,我用0-9代表天干,0-9加ABC代表地支.因此丙辰转换成24,庚申转换成68)

运行结果

posted @ 2026-02-27 06:08  dingxianghuan  阅读(0)  评论(0)    收藏  举报