Given this definition and a test matrix:
data (Eq a, Show a) => QT a = C a | Q (QT a) (QT a) (QT a) (QT a)
    deriving (Eq, Show)
data (Eq a, Num a, Show a) => Mat a = Mat {nexp :: Int, mat :: QT a}
    deriving (Eq, Show)
-- test matrix, exponent is 2, that is matrix is 4 x 4
test = Mat 2 (Q (C 5) (C 6) (Q (C 1) (C 0) (C 2) (C 1)) (C 3))
|     |     |
|  5  |  6  |
|     |     |
-------------
|1 | 0|     |
|--|--|  3  |
|2 | 1|     |
I'm trying to write a function that will output a list of columns sum, like: [13, 11, 18, 18]. The base idea is to sum each sub-quadtree:
- If quadtree is (C c), then output the a repeating2 ^ (n - 1)times the valuec * 2 ^ (n - 1). Example: first quadtree is(C 5)so we repeat5 * 2^(2 - 1) = 10,2 ^ (n - 1) = 2times, obtaining [5, 5].
- Otherwise, given (Q a b c d), wezipWiththe colsum of a and c (and b and d).
Of course this is not working (not even compiling) because after some recursion we have:
zipWith (+) [[10, 10], [12, 12]] [zipWith (+) [[1], [0]] [[2], [1]], [6, 6]]
Because I'm beginning with Haskell I feel I'm missing something, need some advice on function I can use. Not working colsum definition is:
colsum :: (Eq a, Show a, Num a) => Mat a -> [a]
colsum m = csum (mat m)
    where
        n = nexp m
        csum (C c)       = take (2 ^ n) $ repeat (c * 2 ^ n)
        csum (Q a b c d) = zipWith (+) [colsum $ submat a, colsum $ submat b]
                                       [colsum $ submat c, colsum $ submat d]
        submat q = Mat (n - 1) q
Any ideas would be great and much appreciated...
 
                        
Let's consider your
colsum:It is almost correct, except the line where you define
csum (Q a b c d) = ....Let think about types.
colsumreturns a list of numbers.ZipWith (+)sums two lists elementwise:This means that you need to pass two lists of numbers to
zipWith (+). Instead you create two lists of lists of numbers, like this:The type of this expression is
[[a]], not[a]as you need.What you need to do is to concatenate two lists of numbers to obtain a single list of numbers (and this is, probably, what you intended to do):
Similarly, you concatenate lists of partial sums for
canddthen your function should start working.