一点点多项式内插就非常简单!
首先,我查看了短月份名称列表
["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"]
并检查了其字符ASCII值的总和
[313,301,320,323,327,333,331,317,328,326,339,300]
然后从中减去300以了解我在这里要处理的内容,并准备了一个数组,其中包含月份名称的所有较长版本。
[13,1,20,23,27,33,31,17,28,26,39,0]
mons = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
我想您可以看到现在发生了什么-我需要的只是一个getIndex函数,该函数将13映射为0、1映射为1、20映射为2,依此类推,所以我可以简单地执行
getMonthName shortname = mons !! (getIndex shortname)
幸运的是,Wolfram | Alpha 可以为我做到这一点!数字有些大,但是Haskell可以很好地处理。我们只需要确保对结果取整即可,因为浮点算术有点不精确!因此,您可以快速,优雅且惯用的Haskell:
import Data.Char
getIndex x = round $ 11 -
(220797068189915461*x)/11644212222720 +
(184127469431441671621*x^2)/6982771136140800 -
(8800438195450444577647153*x^3)/1013060436431307264000 +
(2826703553741192361967823*x^4)/2026120872862614528000 -
(269098602165195540339443*x^5)/2026120872862614528000 +
(13744405529566098359*x^6)/1692665725031424000 -
(13060656886070844161*x^7)/39727860252208128000 +
(5939638907108115199*x^8)/675373624287538176000 -
(303426664924585177*x^9)/2026120872862614528000 +
(2983240583426137*x^10)/2026120872862614528000 -
(12901227927103*x^11)/2026120872862614528000
mons = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
getMonthName = (mons!!).getIndex.subtract 300.fromIntegral.sum.fmap (ord.toLower)
像这样简单地运行它:
λ> getMonthName "DeC"
"December"
λ> getMonthName "jan"
"January"