我試圖使用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))'
什麼是*矩形問題*? – Ingo 2012-02-02 11:56:37
問題中有一個簡單的矩形和一些基本的功能,對它進行操作.. – 2012-02-02 12:32:51