2011-04-02 52 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