Haskell has the sum function
sum :: Num a => [a] -> a
Which can be nicely composed to sum a matrix by
sum . map sum :: Num a => [[a]] -> a
Going deeper, however, such as summing a cube, creates the restriction Num [a]
sum . map sum . map sum :: (Num a, Num [a]) => [[[a]]] -> a
Which, if you think about it, is natural. So with the former attempt to define the sumcube function blowing up in one’s face, we need to find a different path. One such attempt would be:
sum . map sum . map (map sum) :: Num a => [[[a]]] -> a
Which seems nowhere as natural as the summatrix function.
In my quest to posessing the mental tools for problem solving in Haskell, I am interested in knowing how to tackle this problem of summing a structure of any depth by, say, stacking map sums as in my third code example. Is this at all possible? And in that case, how would you do it?
You’ll have to work from the inside out. When you have a function
ffor summing a data structure, thensum . map fis the way to sum a list of those data structures.