如何创建一个列表,其中包含对 Haskell 中另一个列表的每个项目的计算?

亚历克斯

我正在尝试制作一个函数来计算(然后作为 a 输出String)列表中两个元素之间的差异 - 第 1 个和第 2 个,然后是第 2 个和第 3 个,依此类推 - 我想我很好,但我目前一直遇到错误 whack-a-mole,我把当前的错误放在下面,但首先,强制性的代码转储:

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%

jf_

到目前为止,您只计算列表仅剩两个元素时的百分比。较少的元素没有被覆盖,对于较长的列表,在之前的所有步骤中,元素被删除而无需进一步操作。但是,在最后一步中,您返回的是单个 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"

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

查找/匹配一个列表中另一个列表中每个项目的高效方法

如何从另一个项目列表中过滤一个项目列表?

LINQ选择列表,其中子列表包含另一个列表中的项目

如何使用.count计算一个列表中每个项目出现在python中另一个列表中的次数?

从另一个列表中的列表中的每个列表中提取相同的项目

如何从List <T>检索项目,其中一个属性恰好包含另一个列表中的单词?

根据另一个列表过滤haskell列表

如何检查一个列表是否包含另一个列表子列表中的项目?

在Haskell中,如何将一个列表式单子绑定到另一个列表式单子

Haskell-仅当一个元素等于另一个列表中相同索引的元素时,才添加一个列表中的元素来创建列表

创建大小为“ n”的随机列表,其中包含来自另一个列表的元素

Haskell:创建列表中每个列表的最后一个元素的列表

如何检查另一个列表中是否存在包含一系列项目的列表

用另一个列表Haskell中的元素替换元素列表

将整数列表拆分为2个列表:一个正数,另一个负数-haskell

根据另一个列表返回列表中项目的索引,python

反转Haskell中另一个列表中的列表

haskell如何从另一个列表创建一个新列表?

如何从另一个列表创建项目列表

从另一个列表中的列表中查找项目的索引,即使它们重复

从列表创建一个新的列表,其中包含第一个明确的值

创建一个新列表,其中包含另一个列表中某些单词的以下值

获取包含另一个列表的项目列表

如何计算也出现在另一个列表中的列表中的项目?

如何在 Django 中创建一个用户模型,其中包含一个也是“用户”模型的朋友列表

计算一个列表中的项目数在另一个列表中

计算Python中另一个列表中每个嵌套列表的出现次数

另一个 Haskell 教程中的示例

haskell 中是否有一个函数来确定列表中的每个元素是否在另一个列表中?