I have two lists of tuples which are as follows: [(String,Integer)] and [(Float,Integer)]. Each list has several tuples.
For every Integer that has a Float in the second list, I need to check if its Integer matches the Integer in the first list, and if it does, return the String – although this function needs to return a list of Strings, i.e. [String] with all the results.
I have already defined a function which returns a list of Integers from the second list (for the comparison on the integers in the first list).
This should be solvable using “high-order functions”. I’ve spent a considerably amount of time playing with map and filter but haven’t found a solution!
You have a list of
Integersfrom the second list. Let’s call thisints.Now you need to do two things–first, filter the
(String, Integer)list so that it only contains pairs with corresponding integers in theintslist and secondly, turn this list into just a list ofString.These two steps correspond to the
filterandmaprespectively.First, you need a function to filter by. This function should take a
(String, Integer)pair and return if the integer is in theintslist. So it should have a type of:Writing this should not be too difficult. Once you have it, you can just filter the first list by it.
Next, you need a function to transform a
(String, Integer)pair into aString. This will have type:This should also be easy to write. (A standard function like this actually exists, but if you’re just learning it’s healthy to figure it out yourself.) You then need to map this function over the result of your previous filter.
I hope this gives you enough hints to get the solution yourself.