2015-02-07 94 views
-2

該代碼需要一個四叉樹並插入一個矩形。但是,我得到一個語法分析錯誤。我想調用函數newExtent而不必添加所有參數。所以newExtent TopLeft而不必撥打newExtent TopLeft extent rectangle。我如何使它工作?解析輸入錯誤'let'

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

更換您的讓利塊*請*閱讀[哈斯克爾教程(http://stackoverflow.com/a/1016986/925978)。你不能在'let'語句中輸入一個類型簽名。 – crockeea 2015-02-07 14:20:20

+0

謝謝,我修正了,但我仍然得到相同的錯誤... – quackAttack 2015-02-07 14:22:38

回答

1
  1. 不要縮進插入
  2. 的類型簽名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 
    
+0

謝謝!但它仍然不起作用。我仍然需要調用newExtent'newExtent TopLeft區域矩形',是不是有辦法解決它,所以'newExtent TopLeft'? – quackAttack 2015-02-07 14:47:03

+0

對不起,我不知何故錯過了你的問題,更新了我的答案。 – cocreature 2015-02-07 14:51:50

+0

而不是在where子句中對範圍進行模式匹配,您可以直接在主函數定義中模式匹配。 – cocreature 2015-02-07 14:52:43