首页 > 解决方案 > 如何使用 zipWith(+) 添加两个数据类型 [[double]] 列表?

问题描述

我在 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]]

标签: haskellzipwith

解决方案


zipWith (zipWith (+))

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


推荐阅读