Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7565123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:04:15+00:00 2026-05-30T14:04:15+00:00

I want to conver bibtex entry into my own custom data type, which looks

  • 0

I want to conver bibtex entry into my own custom data type, which looks something like:

date MyEntry = MyEntry {
    ident :: String,
    author :: [String],
    address :: String
}

But fields like author and address are stored in the bibtex’s fields array:

data T =
Cons {
   entryType :: String,
   identifier :: String,
   fields :: [(String, String)]
}
deriving (Show)

An example of the above would be something like:

Cons {entryType = “Book”, identifier = “Arrighi”, fields = [(“author”,
“Arrighi, Gino”),(“title”,”Leonardo Fibonacci : La Pratica di
Geometria. (Volgar izzata da Cristofano di Gherardo di Dino, cittadino
pisano. Dal Codice 2186 dell a Biblioteca Riccardiana di Firenze.) / A
cura e con introduzione di Gino Arrigh
i.”),(“address”,”Pisa”),(“publisher”,”Domus
Galilaeana”),(“date”,”1966″),(“note” ,”(Testimonianze di storia della
scienza, 3)”),(“language”,”italian”),(“pagetota
l”,”234″),(“hyphenation”,”italian”)]}

How can I pattern match on it to convert it to my data type?

I got stuck immediately (this doesn’t work):

toEntry  Cons { @entryType, @ident, @fields } = toEntry' entryType' ident fields'

toEntry' entryType ident fs = MyEntry { ident, entryType, ???????? }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:04:16+00:00Added an answer on May 30, 2026 at 2:04 pm

    You can pattern match on records as described by @ehird, but I think you have also asked about how to convert “fields” list into your “author” and “address” fields.

    You can use standard Prelude function “lookup” for this. It works as follows:

    lookup :: k -> [(k, v)] -> Maybe v
    lookup "a" [("a", 1), ("b", 2)] = Just 1
    lookup "c" [("a", 1), ("b", 2)] = Nothing
    

    It may return “Nothing” if key is not found or “Just value” otherwise.

    So you may convert between these two data types with something like this:

    convert :: T -> MyEntry
    convert (Cons e i fields) = MyEntry i [auth] addr
      where auth = fromMaybe "" (lookup "author" fields)
            addr = fromMaybe "" (lookup "address" fields)
    

    Here, you convert Nothing into an empty string, which is generally not a good idea. Therefore, you may need to change type of address from “String” to “Maybe String” to consider the fact that address may be missing from the original fields.

    Moreover, you may have experience problems with multiple authors, since “lookup” returns the first matching key’s value and that’s all. To handle multiple “author” fields, you may either write your custom lookup function or convert fields list into a Data.Map, concatenating the values of the same key. Here is how (I have changed address type to list, in order to allow multiple addresses as well):

    data MyEntry = MyEntry {
        ident :: String,
        author :: [String],
        address :: [String]
    }
    
    convert :: T -> MyEntry
    convert (Cons e i fields) = MyEntry i auth addr
      where
        fieldsMap = fieldsToMap fields
        auth = lookupField "author" fieldsMap
        addr = lookupField "address" fieldsMap
    
    -- lookup field and return an empty list if not found
    lookupField :: String -> Map.Map String [String] -> [String]
    lookupField = Map.findWithDefault []
    
    -- convert each value into a list and then turn into a map,
    -- concatenating values with the same keys
    fieldsToMap :: [(String, String)] -> Map.Map String [String]
    fieldsToMap = Map.fromListWith (++) . map (\ (k, v) -> (k, [v]))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert a data string into a timestamp (which would include the
I want to convert my single file program - which contains the data (
I want convert string value into date in my project because I am fetching
i have a prolog list like this: [p(X,Y,Z),r(H,G,K)] and i want convert it into
I have a file with letters stored like this \u0410\u0411\u0426 I want to conver
I have to following SQL Statement that I want to conver to LINQ Select
I want to convert a view ID into a URL using EL. The purpose
I want to convert JSON into java code. My jsoncode as per given below.
I want to convert a string into a double and after doing some math
I have an array of Car objects I want to conver them to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.