【问题标题】:How to add two list of data type [[double]] using zipWith(+)?如何使用 zipWith(+) 添加两个数据类型 [[double]] 列表?
【发布时间】:2019-03-29 11:59:39
【问题描述】:

我在 Haskell 中这样做。我正在尝试添加两个要收集的列表,并且我正在使用 zipWith 函数来执行此操作。但是数据类型与我的 add 函数不匹配。

这是我尝试过的

add :: [[Double]] -> [[Double]] -> [[Double]]
add = zipWith []
where zipWith :: (a -> b) -> [a] -> [b]
zipWith _ [] = []
zipWith [] _ = []
zipWith (+) (x:xs) (y:ys) = (+) x y : zipWith (+) xs ys

我想添加两个这样的列表

add [[1,2],[3,4]] [[10,20],[30,40]]
    [[11,22],[33,44]]

【问题讨论】:

  • 你为什么要自己定义zipWith 而不是只使用普通的?为什么使用(+) 作为变量名?这很令人困惑。

标签: haskell zipwith


【解决方案1】:
zipWith (zipWith (+))

我认为不需要进一步解释?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-03
    • 2019-05-29
    • 1970-01-01
    • 2016-10-20
    • 2019-03-01
    • 1970-01-01
    • 2022-01-25
    • 2011-09-24
    相关资源
    最近更新 更多