代码采用四叉树并插入一个矩形。但是我在let statment上得到一个解析错误。我想调用函数newExtent而不必添加所有参数。所以newExtent TopLeft
而不是必须致电newExtent TopLeft extent rectangle
。我如何使它工作?
insert :: QuadTree -> Rectangle -> QuadTree
insert (Qt extent horizontal vertical qTL qTR qBL qBR) rectangle
| quadPart extent rectangle == VerLine = Qt extent horizontal (rectangle:vertical) qTL qTR qBL qBR
| quadPart extent rectangle == HorLine = Qt extent (rectangle:horizontal) vertical qTL qTR qBL qBR
| quadPart extent rectangle == TopLeft && qTL == EmptyQuadTree = Qt extent horizontal vertical (newExtent TopLeft) qTR qBL qBR
| quadPart extent rectangle == TopLeft = Qt extent horizontal vertical (insert qTL rectangle) qTR qBL qBR
| quadPart extent rectangle == TopRight && qTR == EmptyQuadTree = Qt extent horizontal vertical qTL (newExtent TopRight) qBL qBR
| quadPart extent rectangle == TopRight = Qt extent horizontal vertical qTL (insert qTR rectangle) qBL qBR
| quadPart extent rectangle == BottomLeft && qBL == EmptyQuadTree = Qt extent horizontal vertical qTL qTR (newExtent BottomLeft) qBR
| quadPart extent rectangle == BottomLeft = Qt extent horizontal vertical qTL qTR (insert qBL rectangle) qBR
| quadPart extent rectangle == BottomRight && qBR == EmptyQuadTree = Qt extent horizontal vertical qTL qTR qBL (newExtent BottomRight)
| otherwise = Qt extent horizontal vertical qTL qTR qBL (insert qBR rectangle)
let (Rect eL eT eR eB) = extent
in newExtent :: TreeParts -> Rectangle -> Rectangle -> QuadTree
newExtent part (Rect eL eT eR eB) rectangle
| part == TopLeft = insert (emptyQtree (Rect eL eT ((eL + eR) `div` 2) ((eT + eB) `div` 2))) rectangle
| part == TopRight = insert (emptyQtree (Rect ((eL + eR) `div` 2) eT eR ((eT + eB) `div` 2))) rectangle
| part == BottomLeft = insert (emptyQtree (Rect eL ((eT + eB) `div` 2) ((eL + eR) `div` 2) eB)) rectangle
| part == BottomRight = insert (emptyQtree (Rect ((eL + eR) `div` 2) ((eT + eB) `div` 2) eR eB)) rectangle
答案 0 :(得分:1)
您没有在in
之后的表达式中定义函数。一个让
声明允许您定义您可以在中使用的内容
在in
之后只有那里的表达。在这种情况下,where
语句可能会更容易。与这里的let语句相对应,你可以在函数定义之后定义事物,并且可以在函数中使用它们,这看起来就像你想要做的那样。所以尝试用
where (Rect eL eT eR eB) = extent
newExtent part
| part == TopLeft = insert (emptyQtree (Rect eL eT ((eL + eR) `div` 2) ((eT + eB) `div` 2))) rectangle
| part == TopRight = insert (emptyQtree (Rect ((eL + eR) `div` 2) eT eR ((eT + eB) `div` 2))) rectangle
| part == BottomLeft = insert (emptyQtree (Rect eL ((eT + eB) `div` 2) ((eL + eR) `div` 2) eB)) rectangle
| part == BottomRight = insert (emptyQtree (Rect ((eL + eR) `div` 2) ((eT + eB) `div` 2) eR eB)) rectangle