我是haskell的新手,我正在尝试同时学习hspec。
module ExercisesSpec where
import Test.Hspec
import Test.QuickCheck
import Control.Exception (evaluate)
halve :: [a] -> ([a], [a])
halve xs = splitAt (length xs `div` 2) xs
main :: IO ()
main = hspec $ do
describe "halve" $ do
it "0 elements" $ do
halve [] `shouldBe` ([],[])
it "1 element" $ do
halve [1] `shouldBe` ([],[1])
it "2 elements" $ do
halve [1,2] `shouldBe` ([1],[2])
it "3 elements" $ do
halve [1,2,3] `shouldBe` ([1],[2,3])
it "4 elements" $ do
halve [1,2,3,4] `shouldBe` ([1,2],[3,4])
尽管其余的测试都通过了,但对 0 元素的测试却失败了。
No instance for (Show a0) arising from a use of ‘shouldBe’
The type variable ‘a0’ is ambiguous
Note: there are several potential instances:
instance Show Double -- Defined in ‘GHC.Float’
instance Show Float -- Defined in ‘GHC.Float’
instance (Integral a, Show a) => Show (GHC.Real.Ratio a)
-- Defined in ‘GHC.Real’
...plus 38 others
In a stmt of a 'do' block: halve [] `shouldBe` ([], [])
In the second argument of ‘($)’, namely
‘do { halve [] `shouldBe` ([], []) }’
In a stmt of a 'do' block:
it "0 elements" $ do { halve [] `shouldBe` ([], []) }
当我在 ghci 中尝试时,它工作正常。
*Exercises> halve []
([],[])
有人能帮我吗?