0

I am currently trying to filter a list , that consist of tuples of the form (String , Double), for a List that consists of Strings. If the tuple doesn't contain a String of the second list, it should be removed from the list of tuples. So far I came up with this:

test :: [ExamScore] -> String -> [ExamScore]
test a b = filter ((== b).fst) a

My current problem is to replace the String that is filtered for by a list of Strings. Thanks for your help! Please go easy on me, I'm a first year informatics student that hasn't coded anytime before.

1
  • 2
    please show an example call and its desired result. Commented Nov 23, 2019 at 11:32

1 Answer 1

0

It's almost the same, just another filter function using elem:

test a b :: [ExamScore] -> [String] -> [ExamScore]
test a b = filter (\(s, _) -> elem s b) a

Or, if you're more into the composition style:

test a b = filter (flip elem b . fst) a

(It's worth noting that it's not the most efficient way, since elem is O(N) for lists, so depending on your case you may want to find a better structure for storing the keys.)

Sign up to request clarification or add additional context in comments.

1 Comment

The inner parentheses in the second definition are unneeded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.