我的名单上有两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).
如果有人能够向我提出解决办法,那将给我带来巨大帮助。
我的名单上有两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
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 ...
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....
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: ...
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)...
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 ...
The type of the Prelude function flip is: flip :: (a -> b -> c) -> b -> a -> c I.e., it takes one binary function and two arguments. The type of the Prelude function id is: id :: a -...
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 ...
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?