Doubt, haskell error

0

I want to sum the values of a list of lists in haskell using the map function, but of the error. Error type

gastoEmpresa :: [[Float]] -> Float
gastoEmpresa xss = map(map sum xss) 

    
asked by anonymous 01.04.2018 / 15:31

1 answer

0

The type of function map is

map :: (a -> b) -> [a] -> [b]

i.e. map is a function receives as a function a function from a to b and a list of type a and returns a list of type b .

Knowing this makes it easy to see the reason for the error message. In your second application, map gets fewer arguments / parameters than expected (map is applied to too few arguments).

First, remove the second application from the map function and see the partial result.

Be l the list [[1, 4], [2, 3]] , the result of map sum l is

[5, 5]

We have thus been able to create a list in which each element is the sum of the elements of the sub-lists. What we need now is not to apply the map function the second time, but only to a function that receives a list and returns the sum of all its elements: function sum

Putting it all together, a possible implementation would be:

gastoEmpresa :: [[Float]] -> Float
gastoEmpresa l = sum ( map sum l )

Or in pointfree style:

gastoEmpresa :: [[Float]] -> Float
gastoEmpresa = sum . map sum
    
09.04.2018 / 23:56