Michael K
Michael K

Reputation: 13

Using a list of Integers to retrieve a list of Tuples in Haskell

Here is the layout of my problem:

ids :: [Integer]  -- A list of Integers.                                                

db :: [(Integer, Name)]  -- A list of integer ids with the corresponding names

How do I retrieve every tuple from the db where the id from the tuple matches at least one integer from my ids list?

Thanks!

Upvotes: 1

Views: 103

Answers (1)

radrow
radrow

Reputation: 7159

You may use filter function:

query = filter (\t -> fst t `elem` ids) db

or more "elegant" pointfree version

query = filter ((`elem` ids) . fst) db

If you like list comprehensions use

[t | t <- db, fst t `elem` ids]

Upvotes: 4

Related Questions