gpt4 book ai didi

list - Haskell - 使用 foldr/foldl 对列表列表求和

转载 作者:行者123 更新时间:2023-12-05 01:30:07 25 4
gpt4 key购买 nike

我正在尝试编写函数来对列表列表求和(输入 Integer )。我已经使用 map 完成了:

summat :: [[Integer]] -> Integer
summat (x:xs) = sum . map sum $ (x:xs)

但不知道如何使用 foldr 让它工作/ foldl .我试过类似的东西:
summat2 :: [[Integer]] -> Integer
summat2 xs = foldr suma 0 xs
where suma x _ = ???

但当然它只会产生很多错误。你对我有什么暗示吗?也许是 concat ?

最佳答案

如果你想避免任何其他作用于集合的多个元素的函数,试试这个

foldr ((+) . foldr (+) 0) 0

这仅使用 (+)、foldr 和 (.)。

关于list - Haskell - 使用 foldr/foldl 对列表列表求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20669295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com