English 中文(简体)
计算浮雕中 values子的数值
原标题:Calculating the sum of values in a list of tuples in haskell
  • 时间:2011-11-12 18:21:29
  •  标签:
  • haskell

我的名单上有两les。 例如:[(A”、“100”、“B”、“50、3)]。 我需要把每一图的第二和第三部分合并起来,总和显示。 例如:

100* 2 = 200 (add this to a variable),
50* 3 = 150 (add this to the same variable),
Grant total = 350 (I want to display only this).

如果有人能够向我提出解决办法,那将给我带来巨大帮助。

最佳回答

foldl (a (x, y, z) -> a + y*z) 0 [("A", 100, 2), ("B", 50, 3)]

(0 + 100 * 2) + 50 * 3)

问题回答
λ> sum $ map ((_,y,z) -> y*z ) [("A",100,2),("B",50,3)]
350

<>UPD:试图添加一些解释

因此,我们有一个图形<代码>(“A”),100,2)。 我们需要获得第二和第三部分的产品? anonymous function

λ> ((x,y,z) -> y*z) ("A",100,2)
200

X是无法使用的,因此我们能够通过。

λ> ((_,y,z) -> y*z) ("A",100,2)
200

然后,我们应当将这一功能适用于此类拖les名单:map

λ> map ((_,y,z) -> y*z) [("A",100,2),("B",50,3)]
[200,150]

最后一点是找到<代码>[Int] 和sum/a。

λ> sum (map ((_,y,z) -> y*z) [("A",100,2),("B",50,3)])
350

We can use $(function application) instead of parentheses.

λ> sum $ map ((_,y,z) -> y*z) [("A",100,2),("B",50,3)]
350

Done。

采用核对清单:

*Main> let tuples = [("A",100,2),("B",50,3)]
*Main> sum [x*y | (_,x,y) <- tuples]
350
import Data.List (foldl )
sum_tuple_products = (foldl  (+) 0) . map ((_, y, z) -> y * z)

您可以这样看待这一守则:map, first transformations list of tuples (x, y, z) into a list of products y * z, 然后将 pagesl添加该清单的所有内容。

*Main> sum_tuple_products [("a", 100, 2), ("B", 50, 3)]
350
doit = foldr ((_,b,c) acc -> b*c + acc) 0




相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?