'Haskell array conditions confusion

How do I output city names that had temps higher than 5 on the 4th day (4th index)? I can't figure out the conditions. I've tried a couple of things, but nothing worked. I can get the index of item 5, but I don't know how to apply that to the array of items. I can't just do loops like I would in Java.

data City = City { cityName :: String
                 , temperature :: [Double] 
                 }

city1 = City {cityName = "city1", temperature = [4.50,6.50,5.0,6.48,8.54]}
city2 = City {cityName = "city2", temperature = [6.35,5.12,3.21,3.25,4.56]}
city3 = City {cityName = "city3", temperature = [7.3,5.32,4.4,4.6]}

cities :: [City]
cities = [city1,city2,city3]

getTemperatures (City _ _ temperature) = temperature

test :: City -> Double 
test x = (getTemperatures x)!!5


Solution 1:[1]

You can use filter and map (I am using 3rd index (0-base) here):

main = print $ map cityName $ filter ((>5) . (!!3) . temperature) cities -- ["city1"]

data City = City { cityName :: String, temperature :: [Double] }

city1 = City {cityName = "city1", temperature = [4.50,6.50,5.0,6.48,8.54]}
city2 = City {cityName = "city2", temperature = [6.35,5.12,3.21,3.25,4.56]}
city3 = City {cityName = "city3", temperature = [7.3,5.32,4.4,4.6]}

cities :: [City]
cities = [city1,city2,city3]

The filter filters (from the list cities) elements by taking the temperature part (temperature), take the third element (!!3), and then compare it with 5 (>5).

The map, given the output from filter, then takes the name part (cityName). Finally, print prints the list of city names.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1