2
如何在記錄上進行模式匹配還有更習慣用法嗎?我的代碼看起來不正確。F#記錄中的模式匹配
type Period = AM | PM
type TimeOfDay = {hours : int; minutes : int; p : Period}
let before (tod1 : TimeOfDay, tod2 : TimeOfDay) =
match tod1, tod2 with
| {hours = h1; minutes = m1; p = AM}, {hours = h2; minutes = m2; p = AM} -> (h1, m1) < (h2, m2)
| {hours = h1; minutes = m1; p = PM}, {hours = h2; minutes = m2; p = PM} -> (h1, m1) < (h2, m2)
| {hours = _; minutes = _; p = AM}, {hours = _; minutes = _; p = PM} -> true
| {hours = _; minutes = _; p = PM}, {hours = _; minutes = _; p = AM} -> false
如果移動'Period'是在'TimeOfDay',然後'TimeOfDay'值的第一場會自然排序的順序,你期望的,所以你可以用'tod1
如果由於某種原因你不能重新排序記錄字段(例如,如果它們來自外部庫),你仍然可以執行'(tod1.p,tod1.hours,tod1.minutes)<(tod2.p,tod2 .hours,tod2.minutes)'。 – Tarmil
或者,您可以切換到24h時間:'let adjust = function | {小時= H;分鐘=米; p = PM} - > {小時= h + 12;分鐘=米; p = AM} |然後比較這個函數對兩條記錄的應用結果:'let before(tod1,tod2)= adjust tod1
bytebuster