2012-02-02 57 views
0

我试图使用OOHaskell实现矩形问题。OOHaskell中的示例错误示例问题

{-# LANGUAGE EmptyDataDecls, DeriveDataTypeable, TemplateHaskell #-} 
{-# OPTIONS_GHC -fcontext-stack=100 #-} 

module Rectangle where 

import OOHaskell 

$(label "getLength") 
$(label "getWidth") 
$(label "incr") 
$(label "lengthenBy") 
$(label "setLength") 
$(label "setWidth") 

rectangle length width self 
      = do 
      lengthRef <- newIORef value 
       widthRef <- newIORef width 
       return $ 
       getLength .=. readIORef lengthRef 
      .*. getWidth .=. readIORef widthRef 
      .*. setLength .=. writeIORef lengthRef 
      .*. setWidth .=. writeIORef widthRef 
      .*. lengthenBy .=. (\dv -> 
       do 
        value <- self # getValue 
        (self # setValue) (value + dv)) 
      .*. incr  .=. (self # (lengthenBy 1)) 
      .*. emptyRecord 

但我得到范围错误。错误消息是

Rectangle.hs:21:38: Not in scope: `widthRef' 
Rectangle.hs:22:39: Not in scope: `lengthRef' 
Rectangle.hs:23:39: Not in scope: `widthRef' 

我该如何解决错误?

谢谢丹尼尔做到了。但现在我得到的错误是:

The function `lengthenBy' is applied to one argument, 
but its type `Proxy LengthenBy' has none 
In the second argument of `(#)', namely `(lengthenBy 1)' 
In the second argument of `(.=.)', namely `(self # (lengthenBy 1))' 
In the first argument of `(.*.)', namely 
    `incr .=. (self # (lengthenBy 1))' 
+0

什么是*矩形问题*? – Ingo 2012-02-02 11:56:37

+0

问题中有一个简单的矩形和一些基本的功能,对它进行操作.. – 2012-02-02 12:32:51

回答

3

解决您的缩进:

 = do 
     lengthRef <- newIORef value 
      widthRef <- newIORef width 

的的lengthRef“L”和的widthRef“W”必须是在同一列。

因为它的立场,它解析为

= do lengthRef <- newIORef value widthRef <- newIORef ... 

但是,我认为,应该产生一个语法错误,并且未达到“不在范围内”的阶段。所以我想这不是在你的实际代码中,而是在这里出现粘贴故障。

然后:

  return $ 
      getLength .=. readIORef lengthRef 
     .*. getWidth .=. readIORef widthRef 
     .*. setLength .=. writeIORef lengthRef 
     .*. setWidth .=. writeIORef widthRef 
     .*. lengthenBy .=. (\dv -> 
      do 
       value <- self # getValue 
       (self # setValue) (value + dv)) 
     .*. incr  .=. (self # (lengthenBy 1)) 
     .*. emptyRecord 

休息吧,.*.缩进低于DO-块缩进,所以它解析为

(do ... 
    getLength .=. readIORef lengthRef) .*. getWidth .=. ... 

缩进的东西,你想要去的return靠内侧比封闭的do-block要好。

+0

从来没有使用OOHaskell,所以我不知道什么都是类型,但是从错误信息中,可能的原因是它应该是' ((self#lengthenBy)1)''而不是'(self#(lengthenBy 1))''。 – 2012-02-02 11:53:51