简体   繁体   中英

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.

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.

Example

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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