我如何总结Haskell中列表中的值? [英] How can I sum up the values in lists within lists in Haskell?
问题描述
当我试图学习Haskell只是为了好玩,我没有其他的可能,只能问你一个初学者的愚蠢问题^^。
我偶然发现如何找到列表中的列表总和,例如[[1,3],[4,7],[2,5]] = [4,11,7],到目前为止我还没有找到解决方案。有人知道Haskell是如何工作的吗?
考虑函数类型,它可能会有所帮助。
您有 [[Int]]
,并且您想要一个函数 [[Int] ] - > [Int]
获得 [Int]
你需要的函数是 map sum :: [[Int]] - >以下代码运行在
ghci
$ b
首先您列出了整数列表。注意在这个例子中,你需要用 [[Int]]
来引导 ghci
来获得你想要的类型,而不是令人困惑的通用 [[1,3],[4,7],[2,5]] :: Num t => [[t]]
Prelude>让xs = [[1,3],[4,7],[2,5]] :: [[Int]]
接下来,您可能已经知道 sum
,让我们为 [Int]
而不是通用的 sum ::(Foldable t,Num a)=> t a - >一个
,这将使类型更容易阅读
Prelude> let sumInts = sum :: [Int] - >诠释
接下来,让我们测试 sumInts
某些元素 xs = [[1,3],[4,7],[2,5]]
前奏> :t sumInts [1,3]
sumInts [1,3] :: Int
Prelude> sumInts [1,3]
4
现在你可以做 sum
列表中的元素,可以将其用于整个列表中,您可以使用 map
前奏> :t map
map ::(a - > b) - > [a] - > [b]
让我们看看您是否传递 sumInts
到地图你会得到什么类型?
Prelude> :t map sumInts
map sumInts :: [[Int]] - > [Int]
这应该适用于 xs :: [[Int]]
但首先检查类型以确定
Prelude> :t map sumInts xs
map sumInts xs :: [Int]
现在做计算
Prelude> map sumInts xs
[4,11,7]
由于 sumInts = sum
,这也意味着 sum
也可以工作
前奏> :t map sum xs
map sum xs :: [Int]
Prelude> map sum xs
[4,11,7]
注1: map sum real type是
map sum ::(Foldable t,Num b)=> [t b] - > [b]
)在上一个例子中,它是通过xs的类型 [[Int]]
来推断的 [[Int ]] - > [Int]
As I try to learn Haskell just for fun and the experience I have no other possibility than to ask you the "dumb" questions of a total beginner ^^. I stumbled across the problem how it might be possible to find the sums of lists whithin lists e.g. [[1,3],[4,7],[2,5]] = [4,11,7] and I have found no solution so far. Does anybody have a clue how this might work in Haskell?
Think about function types, it may help.
You have [[Int]]
and you want a function [[Int]] -> [Int]
to get [Int]
The function you want is map sum :: [[Int]] -> [Int]
The following codes was run in ghci
First you have list of list of ints. Note that in this example you need to guide ghci
with [[Int]]
to get the type you want instead of the confusing generic [[1,3],[4,7],[2,5]] :: Num t => [[t]]
Prelude> let xs = [[1,3],[4,7],[2,5]] :: [[Int]]
Next, you might already know sum
, let make one that specific for [Int]
instead of generic sum :: (Foldable t, Num a) => t a -> a
, this will make the type easier to read
Prelude> let sumInts = sum :: [Int] -> Int
Next, let test the sumInts
on some element of xs = [[1,3],[4,7],[2,5]]
Prelude> :t sumInts [1,3]
sumInts [1,3] :: Int
Prelude> sumInts [1,3]
4
Now you can do sum
on an element of a list, to do that to entire list you can use map
Prelude> :t map
map :: (a -> b) -> [a] -> [b]
Let see if you pass sumInts
to a map what type you will get?
Prelude> :t map sumInts
map sumInts :: [[Int]] -> [Int]
This should work with xs :: [[Int]]
but let check type first to make sure
Prelude> :t map sumInts xs
map sumInts xs :: [Int]
Now do the computation
Prelude> map sumInts xs
[4,11,7]
Since sumInts = sum
, this also mean sum
would work too
Prelude> :t map sum xs
map sum xs :: [Int]
Prelude> map sum xs
[4,11,7]
Note 1: map sum
real type is map sum :: (Foldable t, Num b) => [t b] -> [b]
) in the last example it was inferred by type [[Int]]
of xs to [[Int]] -> [Int]
这篇关于我如何总结Haskell中列表中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!