-3

The code takes a quadtree and inserts a rectangle. However I get a parse error on the let statment. I want to call the function newExtent without having to add all the arguments. So newExtent TopLeft instead of having to call newExtent TopLeft extent rectangle. How do I make it work?

    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
dfeuer
  • 48,079
  • 5
  • 63
  • 167

1 Answers1

1
  1. Don't indent the type signature of insert
  2. You don't define functions in the expression after in. A let statement allows you do define things you can then use in the expression after in and only there. In this case a where statement would probably be easier. In constrast to the let statement here you define things after the definition of a function and you can use them in the function which seems like what you are trying to do. So try replacing your let block by

    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
    
cocreature
  • 801
  • 5
  • 5
  • Thanks! However it is still doesn't work. I still have to call newExtent with `newExtent TopLeft extent rectangle`, isn't there a way to fix it so `newExtent TopLeft`? – quackAttack Feb 07 '15 at 14:47
  • Sorry, I somehow missed that part from your question, updated my answer. – cocreature Feb 07 '15 at 14:51
  • Instead of pattern matching on extent in the where clause you could also just pattern match in the main function definition directly. – cocreature Feb 07 '15 at 14:52