简体   繁体   English

Haskell数据类型记录到列表

[英]Haskell data type of records to a list

I have a data type in Haskell, which I want to convert into a list of tuples. 我在Haskell中有一个数据类型,我想将其转换为元组列表。

My structure: 我的结构:

 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.")]

How can I do this? 我怎样才能做到这一点?

Thanks, 谢谢,

If it's just for this one then you can easily hard-code it: 如果仅用于此用途,则可以轻松对其进行硬编码:

toTupleList :: Projectdocs -> [(String,String)]
toTupleList pd = 
    [ ("doctype" , docType pd)
    , ("entityID", entityID pd)
    , ("docURL"  , docURL pd)
    ]

Note that your proposed initial element "Projectdocs" doesn't have the type (String, String) , so it cannot be a part of the list. 请注意,建议的初始元素"Projectdocs"没有类型(String, String) ,因此它不能成为列表的一部分。

Example

λ> toTupleList $ Projectdocs {docType="txt", entityID="11012", docURL="www."}
[("doctype","txt"),("entityID","11012"),("docURL","www.")]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM