2

I want to sum Doubles with the Data.Map.map function.

So i want to have a function like this:

sumTheDoubles :: Mapping.Map Char [(Char, Double)] -> Mapping.Map Char [(Char, Double)]

I want to have a sum all Doubles with the Data.Map.map function:

lets say

sumTheDoubles (fromList [('i', [('a', 1.0), ('n', 2.0)])])

should do: ​1.0 + 2.0 -->

(fromList [('i', [('a', 3.0), ('n', 3.0)])])

Yet my main problem is, that I do not really understand how to access the Doubles with the Data.Map.map function.

4
  • 5
    This is an odd signature for a function that sums. Surely you should implement it as a simple -> Double result, and then replacing that value into all the tuples can be a separate task. Jan 12, 2022 at 21:56
  • 3
    You can use Data.Map.map to replace the value once you've calculated it, but it's not really appropriate for doing the summing. Jan 12, 2022 at 22:17
  • 1
    @leftaroundabout But I wonder how I can give a double, if I need plenty of them. Because in the example the size of the map is 1, but can of course be higher than 1. So i should use a list, and then replace fst element of the List with the first Element of the Map?
    – Sonler123
    Jan 12, 2022 at 22:34
  • 1
    Well, maybe it should be Mapping.Map Char Double, if you sum for each element in the Map separately (not clear from the question). But not Mapping.Map Char [(Char, Double)]. Jan 13, 2022 at 9:49

2 Answers 2

2

Gradually, I got to this oneliner:

sumTheDoubles' f = Mapping.map (\a -> map ((\b -> \(c,d) -> (c,b)) ((sum.map snd) a)) a) f

How did I get this result? I started at the highest level. Then I defined more and more functions as needed. Always I first determined the type of individual function. Finally, I converted the functions to lambda form (from the bottom) and applied them to the top level.

module Main where

import qualified Data.Map as Mapping

sumTheDoubles :: Mapping.Map Char [(Char, Double)] -> Mapping.Map Char [(Char, Double)]
sumTheDoubles a = Mapping.map map2 a

-- combine sum and replace
map2 :: [(Char, Double)] -> [(Char, Double)]
map2 a = map (map3 $ map4 a) a

-- replace snd
map3 :: Double -> (Char, Double) -> (Char, Double)
map3 a (b,c) = (b,a)

-- sum Doubles
map4 :: [(Char, Double)] -> Double
map4 a = (sum.map snd) a

main = putStrLn $ show $ sumTheDoubles $ Mapping.fromList [('i', [('a', 1.0), ('n', 2.0)])]

Output:

fromList [('i',[('a',3.0),('n',3.0)])]
1

It does not seem to make much sense to construct a Map Char [(Char, Double)]: the second item of all the 2-tuples in the list will be the same. It makes more sense to construct a Map Char Double, where we thus have summed up the second item of the 2-tuples of the list of values.

We can construct such map with:

sumTheDoubles :: Mapping.Map Char [(Char, Double)] -> Mapping.Map Char Double
sumTheDoubles = fmap (sum . map snd)

we here make use of fmap :: Functor f => (a -> b) -> f a -> f b to apply a function to all values in the Map, and for each of these values we first construct a list of second items of the original list, and then use sum to sum these together.

If you really want to use a list of elements, you can work with:

sumTheDoubles :: Mapping.Map Char [(Char, Double)] -> Mapping.Map Char Double
sumTheDoubles = fmap (\xs -> map (sum (map snd xs) <$) xs)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.