如何创建一个列表,其中包含对 Haskell 中另一个列表的每个项目的计算?
How can you create a list comprising of calculations on each item of another list in Haskell?
我正在尝试使一个函数计算出(然后输出为 String
)列表中两个元素之间的差异 - 第一个和第二个,然后是第二个和第三个,依此类推 - 我认为我正在努力,但我目前一直 运行 陷入错误打地鼠,我已将当前错误放在下面,但首先,强制性代码转储:
type Name = String
type Coordinates = (Int, Int)
type Pop = Int
type TotalPop = [Pop]
type City = (Name, (Coordinates, TotalPop))
testData :: [City]
testData = [("New York City", ((1,1), [5, 4, 3, 2])),
("Washingotn DC", ((3,3), [3, 2, 1, 1])),
("Los Angeles", ((2,2), [7, 7, 7, 5]))]
getPopGrowth :: [City] -> Name -> String
getPopGrowth cs name = concat
[getPercentages z ++ "\n" | (x,z) <- maybeToList (lookup name cs)] where
getPercentages z = unwords (map show z1) ++ "% " where
z1 = percentageIncrease z
percentageIncrease :: [Int] -> [Float]
percentageIncrease (x:xs)
| length (x:xs) > 2 = percentageIncrease (tail xs)
| otherwise = (a / b - 1) * 100.0 where
a = fromIntegral x :: Float
b = fromIntegral (head xs) :: Float
我现在遇到的错误是:
error:
• Couldn't match expected type ‘[Float]’ with actual type ‘Float’
• In the expression: (a / b - 1) * 100.0
In an equation for ‘percentageIncrease’:
percentageIncrease (x : xs)
| length (x : xs) > 2 = percentageIncrease (tail xs)
| otherwise = (a / b - 1) * 100.0
where
a = fromIntegral x :: Float
b = fromIntegral (head xs) :: Float
|
92 | | otherwise = (a / b - 1) * 100.0 where
| ^^^^^^^^^^^^^^^^^^^
我想强调一下,我理解这个错误,但我不知道如何解决它才能获得函数的预期结果。
只是为了清楚地了解我正在尝试做的事情。
输入:getPopGrowth testData "New York City"
应该输出:25% 33.333% 50%
到目前为止,您只计算列表恰好剩下两个元素时的百分比。较少的元素不会被覆盖,对于较长的列表,在之前的所有步骤中,元素会在没有进一步操作的情况下被删除。但是,在最后一步中,您 return 一个 Float 而不是一个列表。
以下示例在每个步骤中创建一个增加百分比,将其与将函数应用于列表尾部的结果列表连接起来。此外,基本情况都已涵盖:
percentageIncrease :: [Int] -> [Float]
percentageIncrease [] = []
percentageIncrease (x:[]) = []
percentageIncrease (x:y:xs) = ((a / b - 1) * 100.0) : percentageIncrease (y:xs) where
a = fromIntegral x :: Float
b = fromIntegral y :: Float
控制台输出:
*Main> getPopGrowth testData "New York City"
"25.0 33.333336 50.0% \n"
我正在尝试使一个函数计算出(然后输出为 String
)列表中两个元素之间的差异 - 第一个和第二个,然后是第二个和第三个,依此类推 - 我认为我正在努力,但我目前一直 运行 陷入错误打地鼠,我已将当前错误放在下面,但首先,强制性代码转储:
type Name = String
type Coordinates = (Int, Int)
type Pop = Int
type TotalPop = [Pop]
type City = (Name, (Coordinates, TotalPop))
testData :: [City]
testData = [("New York City", ((1,1), [5, 4, 3, 2])),
("Washingotn DC", ((3,3), [3, 2, 1, 1])),
("Los Angeles", ((2,2), [7, 7, 7, 5]))]
getPopGrowth :: [City] -> Name -> String
getPopGrowth cs name = concat
[getPercentages z ++ "\n" | (x,z) <- maybeToList (lookup name cs)] where
getPercentages z = unwords (map show z1) ++ "% " where
z1 = percentageIncrease z
percentageIncrease :: [Int] -> [Float]
percentageIncrease (x:xs)
| length (x:xs) > 2 = percentageIncrease (tail xs)
| otherwise = (a / b - 1) * 100.0 where
a = fromIntegral x :: Float
b = fromIntegral (head xs) :: Float
我现在遇到的错误是:
error:
• Couldn't match expected type ‘[Float]’ with actual type ‘Float’
• In the expression: (a / b - 1) * 100.0
In an equation for ‘percentageIncrease’:
percentageIncrease (x : xs)
| length (x : xs) > 2 = percentageIncrease (tail xs)
| otherwise = (a / b - 1) * 100.0
where
a = fromIntegral x :: Float
b = fromIntegral (head xs) :: Float
|
92 | | otherwise = (a / b - 1) * 100.0 where
| ^^^^^^^^^^^^^^^^^^^
我想强调一下,我理解这个错误,但我不知道如何解决它才能获得函数的预期结果。
只是为了清楚地了解我正在尝试做的事情。
输入:getPopGrowth testData "New York City"
应该输出:25% 33.333% 50%
到目前为止,您只计算列表恰好剩下两个元素时的百分比。较少的元素不会被覆盖,对于较长的列表,在之前的所有步骤中,元素会在没有进一步操作的情况下被删除。但是,在最后一步中,您 return 一个 Float 而不是一个列表。
以下示例在每个步骤中创建一个增加百分比,将其与将函数应用于列表尾部的结果列表连接起来。此外,基本情况都已涵盖:
percentageIncrease :: [Int] -> [Float]
percentageIncrease [] = []
percentageIncrease (x:[]) = []
percentageIncrease (x:y:xs) = ((a / b - 1) * 100.0) : percentageIncrease (y:xs) where
a = fromIntegral x :: Float
b = fromIntegral y :: Float
控制台输出:
*Main> getPopGrowth testData "New York City"
"25.0 33.333336 50.0% \n"