Home > Blockchain >  How to sum first elements from lists in Haskell?
How to sum first elements from lists in Haskell?

Time:02-20

Function should works like this:

sumFstElems [[1,2],[10,122,1],[12],[0],[1,1000]] = 24

I know that this prints first elements from lists:

fun xs = fmap head xs

This sum elements in list:

mysum (x:xs) = x   mysum xs

I have a problem with combining these two functions to give the expected result.

CodePudding user response:

This is how I would write the function:

sumFstElems :: [[Int]] -> Int
sumFstElems nss = sum (map head nss)

With fun and mysum it can be written as

sumFstElems :: [[Int]] -> Int
sumFstElems nss = mysum (fun nss)

or

sumFstElems :: [[Int]] -> Int
sumFstElems = mysum . fun

CodePudding user response:

Let's consider some basics:

Prelude> lst = [[1, 2], [6], [8, 9]]
Prelude> map head lst
[1,6,8]
Prelude> sum (map head lst)
15

Now, let's make a function:

Prelude> sumFstElems lst = sum (map head lst)
Prelude> sumFstElems lst
15

But what if we use . to compose the functions sum and map head, and then apply that to the lst argument?

Prelude> sumFstElems lst = (sum . map head) lst
Prelude> sumFstElems lst
15

That works. But then we don't need to explicitly list the argument.

Prelude> sumFstElems = sum . map head
Prelude> sumFstElems lst
15

Of course, this will fail if any of the lists contained in lst are empty. Given that this is possible, maybe we should filter those out.

Prelude> sumFstElems = sum . map head . filter (/= [])
Prelude> sumFstElems [[1, 2], [6], [8, 9], [], [4]]
19

A list comprehension provides a way of combining the filtering and mapping.

Prelude> sumFstElems lst = sum [head x | x <- lst, x /= []]
Prelude> sumFstElems [[1, 2], [6], [8, 9], [], [4]]
19
  • Related