我正在学习Haskell并且我已经编写了这个函数:
safeHead :: [a] -> Maybe a
safeHead [] = Nothing
safeHead (x:xs) = Just x
Run Code Online (Sandbox Code Playgroud)
我现在正尝试用HSpec测试它:
import Test.Hspec
main :: IO ()
main = hspec spec
spec :: Spec
spec =
describe "safeHead" $
it "should return Nothing for empty list" $
safeHead [] `shouldBe` Nothing
Run Code Online (Sandbox Code Playgroud)
但是无法编译:
Error:(14, 19) ghc: No instance for (Eq a0) arising from a use of ‘shouldBe’
The type variable ‘a0’ is ambiguous
Note: there are several potential instances:
instance Eq a => Eq (Maybe a) -- Defined in ‘GHC.Base’
instance Eq a => Eq (GHC.Real.Ratio a) -- Defined in ‘GHC.Real’
instance Eq Ordering -- Defined in ‘ghc-prim-0.4.0.0:GHC.Classes’
...plus 31 others
In the second argument of ‘($)’, namely
‘safeHead [] `shouldBe` Nothing’
In the second argument of ‘($)’, namely
‘it "should return Nothing for empty list"
$ safeHead [] `shouldBe` Nothing’
In the expression:
describe "safeHead"
$ it "should return Nothing for empty list"
$ safeHead [] `shouldBe` Nothing
Run Code Online (Sandbox Code Playgroud)
我也试过这个:
safeHead :: (Eq a) => [a] -> Maybe a
safeHead [] = Nothing
safeHead (x:xs) = Just x
Run Code Online (Sandbox Code Playgroud)
但是仍然失败了:
Error:(14, 19) ghc: No instance for (Eq a0) arising from a use of ‘shouldBe’
The type variable ‘a0’ is ambiguous
Note: there are several potential instances:
instance (Eq a, Eq b) => Eq (Either a b)
-- Defined in ‘Data.Either’
instance Eq Data.Monoid.All -- Defined in ‘Data.Monoid’
instance forall (k :: BOX) (f :: k -> *) (a :: k).
Eq (f a) =>
Eq (Data.Monoid.Alt f a)
-- Defined in ‘Data.Monoid’
...plus 43 others
In the second argument of ‘($)’, namely
‘safeHead [] `shouldBe` Nothing’
In the second argument of ‘($)’, namely
‘it "should return Nothing for empty list"
$ safeHead [] `shouldBe` Nothing’
In the expression:
describe "safeHead"
$ it "should return Nothing for empty list"
$ safeHead [] `shouldBe` Nothing
Run Code Online (Sandbox Code Playgroud)
我不知道这里有什么问题.如果我尝试这样的其他测试,它编译很好:
it "should return the head" $ do
safeHead [1] `shouldBe` Just 1
safeHead [2,3,4,5,6,1] `shouldBe` Just 2
Run Code Online (Sandbox Code Playgroud)
那么,它是关于Nothing
它自己的东西,它不能通过平等进行比较?你怎么断言某些东西会回来Nothing
呢?或者我的功能太通用了?
侧面注意:我看到这个函数有类似的错误:
palindrome :: (Eq a) => [a] -> [a]
palindrome xs = xs ++ reverse xs
Run Code Online (Sandbox Code Playgroud)
尝试测试空列表时:
palindrome [] `shouldBe` []
Run Code Online (Sandbox Code Playgroud)
哪个失败了:
Error:(26, 21) ghc: No instance for (Eq a0) arising from a use of ‘shouldBe’
The type variable ‘a0’ is ambiguous
Note: there are several potential instances:
instance Eq a => Eq (Maybe a) -- Defined in ‘GHC.Base’
instance Eq a => Eq (GHC.Real.Ratio a) -- Defined in ‘GHC.Real’
instance Eq Ordering -- Defined in ‘ghc-prim-0.4.0.0:GHC.Classes’
...plus 32 others
In a stmt of a 'do' block: palindrome [] `shouldBe` []
In the second argument of ‘($)’, namely
‘do { palindrome [] `shouldBe` [] }’
In the second argument of ‘($)’, namely
‘it
"should turn a list into a palindrome, so it reads same both forwards and backwards"
$ do { palindrome [] `shouldBe` [] }’
Run Code Online (Sandbox Code Playgroud)
那么,它是关于Nothing本身的东西,它不能通过equals进行比较?
什么Nothing
类型的?是的Nothing :: Maybe a
.GHC a
在这种情况下并不喜欢:"类型变量'a0'是模糊的".毕竟,shouldBe
采取任何可以比较(==)
和显示的东西.并且Maybe a
是Eq
if a
的实例的一个实例Eq
.GHC不可能知道您要使用哪个 a
,因此您需要手动指定:
describe "safeHead" $
it "should return Nothing for empty list" $
safeHead [] `shouldBe` (Nothing :: Maybe Int)
Run Code Online (Sandbox Code Playgroud)
这不是强制,你只是弄清楚你想要使用哪种类型.其他例子:
describe "safeHead" $
it "should return Nothing for empty list" $ do
safeHead [] `shouldBe` (Nothing :: Maybe Int)
safeHead [] `shouldBe` (Nothing :: Maybe ())
safeHead [] `shouldBe` (Nothing :: Maybe Integer)
safeHead [] `shouldBe` (Nothing :: Maybe Char)
Run Code Online (Sandbox Code Playgroud)