我对Haskell很新。 今天当我读这本书并练习它的例子时,我收到了一个错误。 以下是第3章第57页中的源代码Nullable.hs。
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>
我认为这个问题是由范围引起的,所以我在“Just”中添加一个前缀,就像这个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
现在我可以确认它不仅是由范围错误引起的。但我无法处理......
这样做的方法是什么? 任何建议将不胜感激。
答案 0 :(得分:2)
不,原始错误是由范围错误引起的,因此当您明确限定它时,您修复了一个错误但引入了另一个错误。
您可以通过在原始代码中添加deriving (Show)
行来解决其他错误:
data Maybe a = Just a
| Nothing
deriving (Show)