我是一個相當新的Haskell程序員,我試圖弄清楚如何將一些值轉換爲代數數據類型。從列表中初始化代數數據類型
我有一個記錄數據類型:
data OrbitElements = OrbitElements { epoch :: Double,
ecc :: Double,
distPeri :: Double,
incl :: Double,
longAscNode :: Double,
argPeri :: Double,
timePeri :: Double,
meanMotion :: Double,
meanAnomaly :: Double,
trueAnomaly :: Double,
semiMajorAxis :: Double,
distApo :: Double,
period :: Double
}
我拉從一個文本文件,這在雙打名單結束了一些信息。有沒有簡單的方法來初始化這個數據類型與列表?我可以單獨調用每個setter,但是如果我已經擁有列表中的所有值,那麼這看起來非常低效。
let d = [2456382.5,6.786842103348031e-3,0.7184187640759256,3.394660181513041,76.64395338801751,55.2296201483587,2456457.141012543,1.602144936476915,240.4142797010899,239.7408018186761,0.7233278761603762,0.7282369882448266,224.6987721295883]
let o = OrbitElements
let epoch o = d !! 0
let ecc o = d !! 1
-- and so on
我在想什麼?
謝謝定義
fromList
,這證實了我的懷疑,並啓動了任何回答「首先,我們定義兩個解析器......「在我的書中很棒:) –與['-XRecordWildCards'](http://www.haskell.org/ghc/docs/7.4)結合使用時,」手動「 .2/html/users_guide/syntax-extns.html#record-wildcards):'fromList [epoch,ecc,distPeri, incl,longAscNode,argPeri,timePeri,meanMotion,meanAnomaly,trueAnomaly,semiMajorAxis,distApo,period] = Just OrbitElements {..}'。 –