使用数据数组我如何在转换为华氏温度后输出数据数组。 - Haskell
Using data arrays how do i ouput arrays of data after converting to fahrenheit. - Haskell
我想从 cities 数组中的所有数据中获取温度并以华氏度输出数据。
我尝试过的所有方法似乎都不起作用,正如您所见,getAllTempsF 是我最近的尝试。
C 到 F 是 (c*1.8)+18
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]
getAllNames :: [City] -> [String]
getAllNames x = map cityName x
getAllTemps :: [City] -> [[Double]]
getAllTemps x = map temperature x
getAllTempsF :: [City] -> [[Double]]
getAllTempsF [] = 1
getAllTempsF (x:xs) = [((x * 1.8) + 32)..] >> getAllTempsF xs
您已经在getAllTemps
中使用了map
,意思是“对列表的每个元素做一些事情”。 getAllTempsF
也可以 表述为“对列表的每个元素做某事”,在这种情况下,“某事”是一些数学。唯一的区别是我们处理的是列表的列表,而不是单个列表。我们仍然可以使用map
来写这个函数;我们只需要做两次。
getAllTempsF :: [City] -> [[Double]]
getAllTempsF xs = map (map (\x -> x * 1.8 + 32)) $ getAllTemps xs
现在我们在列表列表中有了它。您提到打印出数据,为此我们将使用 forM_
,它与 map
类似,但用于副作用而不是值。
printOutTemps :: [City] -> IO ()
printOutTemps cities = do
-- Get each city, together with its temperatures in a list.
let cityData = zip cities (getAllTempsF cities)
-- For each one... do some IO.
forM_ cityData $ \(city, tempsF) -> do
-- Get the temperatures as a list of strings, so we can print them out.
let tempsStr = map show tempsF
-- Print out the city names and the temperatures.
putStrLn $ (cityName city) ++ " has temperatures " ++ unwords tempsStr
如果您还没有找到此特定资源,Hoogle is a great site for identifying Haskell functions. So if you don't know what any of the functions I used in this answer do, you can always search them on Hoogle. For instance, if you don't know what zip
does, you can search zip
第一个结果正是您要查找的函数。
我想从 cities 数组中的所有数据中获取温度并以华氏度输出数据。
我尝试过的所有方法似乎都不起作用,正如您所见,getAllTempsF 是我最近的尝试。
C 到 F 是 (c*1.8)+18
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]
getAllNames :: [City] -> [String]
getAllNames x = map cityName x
getAllTemps :: [City] -> [[Double]]
getAllTemps x = map temperature x
getAllTempsF :: [City] -> [[Double]]
getAllTempsF [] = 1
getAllTempsF (x:xs) = [((x * 1.8) + 32)..] >> getAllTempsF xs
您已经在getAllTemps
中使用了map
,意思是“对列表的每个元素做一些事情”。 getAllTempsF
也可以 表述为“对列表的每个元素做某事”,在这种情况下,“某事”是一些数学。唯一的区别是我们处理的是列表的列表,而不是单个列表。我们仍然可以使用map
来写这个函数;我们只需要做两次。
getAllTempsF :: [City] -> [[Double]]
getAllTempsF xs = map (map (\x -> x * 1.8 + 32)) $ getAllTemps xs
现在我们在列表列表中有了它。您提到打印出数据,为此我们将使用 forM_
,它与 map
类似,但用于副作用而不是值。
printOutTemps :: [City] -> IO ()
printOutTemps cities = do
-- Get each city, together with its temperatures in a list.
let cityData = zip cities (getAllTempsF cities)
-- For each one... do some IO.
forM_ cityData $ \(city, tempsF) -> do
-- Get the temperatures as a list of strings, so we can print them out.
let tempsStr = map show tempsF
-- Print out the city names and the temperatures.
putStrLn $ (cityName city) ++ " has temperatures " ++ unwords tempsStr
如果您还没有找到此特定资源,Hoogle is a great site for identifying Haskell functions. So if you don't know what any of the functions I used in this answer do, you can always search them on Hoogle. For instance, if you don't know what zip
does, you can search zip
第一个结果正是您要查找的函数。