Haskell 数组条件混乱
Haskell array conditions confusion
如何输出第 4 天(第 4 个索引)温度高于 5 的城市名称?我想不通条件。我尝试了几件事,但没有任何效果。我可以获得项目 5 的索引,但我不知道如何将其应用于项目数组。我不能像在 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
您可以使用 filter
和 map
(我在这里使用第三个索引(0-base)):
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]
filter
通过取温度部分(temperature
)过滤(来自列表cities
)元素,取第三个元素(!!3
),然后将其与 5 (>5
).
进行比较
map
给定 filter
的输出,然后采用名称部分 (cityName
)。最后,print
打印城市名称列表。
如何输出第 4 天(第 4 个索引)温度高于 5 的城市名称?我想不通条件。我尝试了几件事,但没有任何效果。我可以获得项目 5 的索引,但我不知道如何将其应用于项目数组。我不能像在 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
您可以使用 filter
和 map
(我在这里使用第三个索引(0-base)):
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]
filter
通过取温度部分(temperature
)过滤(来自列表cities
)元素,取第三个元素(!!3
),然后将其与 5 (>5
).
map
给定 filter
的输出,然后采用名称部分 (cityName
)。最后,print
打印城市名称列表。