[英]Haskell data type of records to a list
我在Haskell中有一個數據類型,我想將其轉換為元組列表。
我的結構:
data Projectdocs = Projectdocs {
docType :: String,
entityID :: String,
docURL :: String
}deriving Show
--A sample projectdocs type
Projectdocs{docType="txt",entityID="11012",docURL="www."}
--The output I want to create
["Projectdocs"]
[("doctype","txt"),("entityID","11012"),("docURL","www.")]
我怎樣才能做到這一點?
謝謝,
如果僅用於此用途,則可以輕松對其進行硬編碼:
toTupleList :: Projectdocs -> [(String,String)]
toTupleList pd =
[ ("doctype" , docType pd)
, ("entityID", entityID pd)
, ("docURL" , docURL pd)
]
請注意,建議的初始元素"Projectdocs"
沒有類型(String, String)
,因此它不能成為列表的一部分。
λ> toTupleList $ Projectdocs {docType="txt", entityID="11012", docURL="www."}
[("doctype","txt"),("entityID","11012"),("docURL","www.")]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.