我在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.")]
我该怎么做?
谢谢,
答案 0 :(得分:4)
如果它仅适用于此版本,那么您可以轻松地对其进行硬编码:
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.")]