Type Errors when implementing Quadtree

与世无争的帅哥 提交于 2019-12-24 07:38:25

问题


Update:

So this is my code:

quadtreeToPic :: Quadtree -> Array (Int, Int) Word8
quadtreeToPic (QNode x y w avg Q0)
  | w == 1 = listArray (0,0) [avg]
  | w == 2 = listArray (0,4) [avg, avg, avg, avg]

quadtreeToPic (QNode x y w avg (Q4 q1 q2 q3 q4)) = listArray ((0,0), (w-1,w-1)) (concat (map quadtreeToPic [q1, q2, q3, q4])) 

A Quadtree is either

QNode Int Int Int Word8 QKids
data QKids = Q0 | Q4 Quadtree Quadtree Quadtree Quadtree

The error I get is

Quadtree.hs:13:90: error:
    • Couldn't match type ‘Array (Int, Int) Word8’ with ‘[Word8]’
      Expected type: [[Word8]]
        Actual type: [Array (Int, Int) Word8]
    • In the first argument of ‘concat’, namely
        ‘(map quadtreeToPic [q1, q2, q3, q4])’
      In the second argument of ‘listArray’, namely
        ‘(concat (map quadtreeToPic [q1, q2, q3, q4]))’
      In the expression:
        listArray
          ((0, 0), (w - 1, w - 1))
          (concat (map quadtreeToPic [q1, q2, q3, q4]))
   |
13 | quadtreeToPic (QNode x y w avg (Q4 q1 q2 q3 q4)) = listArray ((0,0), (w-1,w-1)) (concat (map quadtreeToPic [q1, q2, q3, q4])) 
   |                                                                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

How do I solve this? Why is [[Word8]] expected when by definition it should expect Array (Int, Int) Word8?


回答1:


It's referring to the underlined subexpression:

concat (map quadtreeToPic [q1, q2, q3, q4])
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The expected type is the type that it should be from context, that is:

concat ???

It's what the compiler knows about what ??? must be without looking inside it. concat :: [[a]] -> [a] (*) takes a collection of lists and joins them all together. So we expect to see a collection of lists.

The actual type is the type that it looks like it is when analyzing the expression "from the inside":

map quadtreeToPic [q1, q2, q3, q4]

Here we get, from map and the declared type signature of your function, a [Array (Int,Int) Word8]. It's a collection of arrays. But we were expecting a collection of lists from the previous step. Thus the error:

Couldn't match type ‘Array (Int, Int) Word8’ with ‘[Word8]’

Perhaps you want to turn those arrays into lists using Data.Foldable.toList first?

(*)Well it's actually concat :: Foldable t => t [a] -> [a] but I simplified for exposition.



来源:https://stackoverflow.com/questions/57277522/type-errors-when-implementing-quadtree

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!