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 9232345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:17:31+00:00 2026-06-18T06:17:31+00:00

I have: type Person = String type Book = String type Database = [(Person,[Book])]

  • 0

I have:

type Person     = String
type Book       = String
type Database   = [(Person,[Book])]

I’m trying to define a function:

books :: Database -> Person -> [Book]

that takes in
1) a list of tuples (which contain a string and a list of strings
2) a String name

and returns a list of strings (namely the books from the database)

I want to use list comprehension, but I don’t know how to access elements within a list that is in the tuple inside of the database list.

Thoughts?

Example database would look like:

db = [("Bob", ["Red Riding Hood", "Alice in Wonderland"]), ("Carol", ["Game of Thrones"])]

And if I ask for say “Carol”, it should return [“Game of Thrones”].

  • 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-06-18T06:17:32+00:00Added an answer on June 18, 2026 at 6:17 am

    Since you have an association list you can use the lookup function. It pretty much works exactly like you want:

    getVal :: Database -> String -> Maybe [String]
    getVal  = lookup
    

    The only difference is that it returns a Maybe but IMHO that is the right behavior, consider what would happen if you didn’t have a value in your database when you looked it up?

    Since you want to use pattern matching here is the source of lookup

    lookup                  :: (Eq a) => a -> [(a,b)] -> Maybe b
    lookup _key []          =  Nothing
    lookup  key ((x,y):xys)
        | key == x          =  Just y
        | otherwise         =  lookup key xys
    

    Or in your case

    getVal :: Database -> String -> [String] --Avoids a Maybe if you want
    getVal _key []          =  []
    getVal  key ((x,y):xys)
        | key == x          =  y
        | otherwise         =  getVal key xys
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a List of type Person that I would like to update DisplayValue
I have an array of type Person, which contains string objects: FirstName, LastName, login.
I have this type which is basically a struct { int x,y,z; } that
I have a object in F# as follows... type Person(?name : string) = let
I'm trying to generate a new list from a type that is essentially a
If I have a class Person which has two instance variables: name of type
Let's say that I have the following code: import Data.List.Ordered data Person = Person
I have an object that takes in a parameter of the same type in
I have integer type field in database which is having property Not Null. when
Let's say I have type Person struct { Name string } func (p *Person)

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.