Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell Map and Filter [closed]

Here is my issue: I have two lists as follows: [(Float, Integer)] and [(String, Integer)]. Now I need to define a function that takes these two Lists and two Float values and returns a list of Strings. The two Float values correspond to a range given (min and max). I have to filter the first list so it only contains elements that are within the min and max range. Then, I need to use the filtered list and take it's Integer values, match them with the Integer values in the second list and return all the String values that match.

I have already defined a function for use as a filter condition that takes a (Float,Integer) and checks the Float value to see if it is within the given range.

And I have defined a function to take a (String,Integer) element and return the String.

I'm just having problems linking everything together, or maybe I am missing something!

like image 423
gdrules Avatar asked Jul 20 '26 21:07

gdrules


1 Answers

You need (for example, there are different ways)

integersFromRange :: Float -> Float -> [(Float,Integer)] -> [Integer]
stringsFromInteger :: Integer -> [(String,Integer)] -> [String]

integersFromRange is basically map snd . filter condition, with condition constructed from the two Floats (you have that). stringsFromInteger could be implemented as map fst . filter condition. Then you combine the functions with

result = concatMap (`stringsFromInteger` stringList) (integerFromRange mini maxi floatList)

Using a set of Integers instead of a list would be more efficient since membership test in a Set is faster than in a list.

like image 51
Daniel Fischer Avatar answered Jul 23 '26 17:07

Daniel Fischer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!