2011-04-02 70 views
1

我對Haskell很新。 今天,當我讀這本書並練習它的例子時,我得到了一個錯誤。 這裏是第3章頁面的源代碼Nullable.hs 57真實世界Haskell示例中的模糊錯誤

import Prelude hiding (Maybe) 

{-- snippet Nullable --} 
data Maybe a = Just a 
      | Nothing 
{-- /snippet Nullable --} 

{-- snippet wrappedTypes --} 
someBool = Just True 

someString = Just "something" 
{-- /snippet wrappedTypes --} 


{-- snippet parens --} 
wrapped = Just (Just "wrapped") 
{-- /snippet parens --} 

當我輸入ghci Nullable.hs 有:

GHCi, version 6.12.3: http://www.haskell.org/ghc/ :? for help 
Loading package ghc-prim ... linking ... done. 
Loading package integer-gmp ... linking ... done. 
Loading package base ... linking ... done. 
Loading package ffi-1.0 ... linking ... done. 
[1 of 1] Compiling Main    (Downloads/examples/ch03/Nullable.hs, interpreted) 

Downloads/examples/ch03/Nullable.hs:9:11: 
    Ambiguous occurrence `Just' 
    It could refer to either `Main.Just', defined at Downloads/examples/ch03/Nullable.hs:4:15 
          or `Prelude.Just', imported from Prelude at Downloads/examples/ch03/Nullable.hs:1:0-28 

Downloads/examples/ch03/Nullable.hs:11:13: 
    Ambiguous occurrence `Just' 
    It could refer to either `Main.Just', defined at Downloads/examples/ch03/Nullable.hs:4:15 
          or `Prelude.Just', imported from Prelude at Downloads/examples/ch03/Nullable.hs:1:0-28 

Downloads/examples/ch03/Nullable.hs:16:10: 
    Ambiguous occurrence `Just' 
    It could refer to either `Main.Just', defined at Downloads/examples/ch03/Nullable.hs:4:15 
          or `Prelude.Just', imported from Prelude at Downloads/examples/ch03/Nullable.hs:1:0-28 

Downloads/examples/ch03/Nullable.hs:16:16: 
    Ambiguous occurrence `Just' 
    It could refer to either `Main.Just', defined at Downloads/examples/ch03/Nullable.hs:4:15 
          or `Prelude.Just', imported from Prelude at Downloads/examples/ch03/Nullable.hs:1:0-28 
Failed, modules loaded: none. 
Prelude> 

所以我添加一個前綴,我認爲造成這一範圍問題「只是」像這樣someBool = Main.Just True,然後再試一次了:

[1 of 1] Compiling Main    (nu.hs, interpreted) 
Ok, modules loaded: Main. 
*Main> Just 1 

<interactive>:1:0: 
    No instance for (Show (Maybe t)) 
     arising from a use of `print' at <interactive>:1:0-5 
    Possible fix: add an instance declaration for (Show (Maybe t)) 
    In a stmt of an interactive GHCi command: print it 

現在我可以證實,這不僅是由範圍錯誤導致。但我無法處理它...

什麼是一種方法來做到這一點? 任何建議將不勝感激。

回答

2

不,原始錯誤是由範圍錯誤引起的,所以當您明確限定它時,您修復了一個錯誤但引入了另一個錯誤。

你可以通過添加一個deriving (Show)線到你原來的代碼修復其他錯誤:

data Maybe a = Just a 
     | Nothing 
    deriving (Show) 
+0

哦,是的......謝謝。 – Pikaurd 2011-04-02 10:09:29