输入“let”上的解析错误

时间:2015-02-07 14:11:22

标签: haskell

代码采用四叉树并插入一个矩形。但是我在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

1 个答案:

答案 0 :(得分:1)

  1. 不要缩进insert
  2. 的类型签名
  3. 您没有在in之后的表达式中定义函数。一个让 声明允许您定义您可以在中使用的内容 在in之后只有那里的表达。在这种情况下,where语句可能会更容易。与这里的let语句相对应,你可以在函数定义之后定义事物,并且可以在函数中使用它们,这看起来就像你想要做的那样。所以尝试用

    替换你的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