如何在 Haskell 堆栈测试中对 Maybe 值使用 putStrLn?
How do I use putStrLn on Maybe value in Haskell Stack test?
我正在使用 Haskell 堆栈创建程序。我的程序构建正常,但我想编写一个测试文件,以便我可以 运行 stack test
在我现有的程序上。我的问题是我的函数测试 returns 一个 Maybe 类型,不能使用语法 putStrLn f
输出。我正在尝试在我的测试文件中编写一个函数,该函数采用 Maybe 值和 returns “Nothing”(如果给定 Nothing)或包含 a 的字符串(如果给定) Just a.
到目前为止,这是我的代码:
printMaybe :: Show a => Maybe a -> String
printMaybe Nothing = "Nothing"
printMaybe (Just a) = show a
并且 main 包含该行
putStrLn $ printMaybe (Nothing)
我的错误信息是:
test\Spec.hs:12:16: error:
* Ambiguous type variable a0' arising from a use of
printMaybe'
prevents the constraint (Show a0)' from being solved. Probable fix: use a type annotation to specify what
a0' should be.
These potential instances exist:
instance Show Ordering -- Defined in GHC.Show' instance Show Integer -- Defined in
GHC.Show'
instance Show a => Show (Maybe a) -- Defined in GHC.Show' ...plus 22 others ...plus 27 instances involving out-of-scope types (use -fprint-potential-instances to see them all) * In the second argument of
($)', namely `printMaybe (Nothing)'
In a stmt of a 'do' block: putStrLn $ printMaybe (Nothing)
In the expression:
do putStrLn ("====== Tests Start ======")
putStrLn $ printMaybe (Nothing)
|
12 | putStrLn $ printMaybe (Nothing)
| ^^^^^^^^^^^^^^^^^^^^
任何帮助将不胜感激:)
如错误所述,问题不在于您的 printMaybe
,而在于您的 do
块中的 printMaybe Nothing
,从那时起就不清楚 a
类型是。
因此你可能有一些东西like:K
main = do
putStrLn "====== Tests Start ======"
putStrLn (printMaybe <b>Nothing</b>)
但不清楚 Maybe a
类型的 a
是什么 Nothing
。你可以给编译器一个提示:
main = do
putStrLn "====== Tests Start ======"
putStrLn (printMaybe (Nothing <b>:: Maybe Int</b>))
我正在使用 Haskell 堆栈创建程序。我的程序构建正常,但我想编写一个测试文件,以便我可以 运行 stack test
在我现有的程序上。我的问题是我的函数测试 returns 一个 Maybe 类型,不能使用语法 putStrLn f
输出。我正在尝试在我的测试文件中编写一个函数,该函数采用 Maybe 值和 returns “Nothing”(如果给定 Nothing)或包含 a 的字符串(如果给定) Just a.
到目前为止,这是我的代码:
printMaybe :: Show a => Maybe a -> String
printMaybe Nothing = "Nothing"
printMaybe (Just a) = show a
并且 main 包含该行
putStrLn $ printMaybe (Nothing)
我的错误信息是:
test\Spec.hs:12:16: error: * Ambiguous type variable
a0' arising from a use of
printMaybe' prevents the constraint(Show a0)' from being solved. Probable fix: use a type annotation to specify what
a0' should be. These potential instances exist: instance Show Ordering -- Defined inGHC.Show' instance Show Integer -- Defined in
GHC.Show' instance Show a => Show (Maybe a) -- Defined inGHC.Show' ...plus 22 others ...plus 27 instances involving out-of-scope types (use -fprint-potential-instances to see them all) * In the second argument of
($)', namely `printMaybe (Nothing)' In a stmt of a 'do' block: putStrLn $ printMaybe (Nothing) In the expression: do putStrLn ("====== Tests Start ======") putStrLn $ printMaybe (Nothing) | 12 | putStrLn $ printMaybe (Nothing) | ^^^^^^^^^^^^^^^^^^^^
任何帮助将不胜感激:)
如错误所述,问题不在于您的 printMaybe
,而在于您的 do
块中的 printMaybe Nothing
,从那时起就不清楚 a
类型是。
因此你可能有一些东西like:K
main = do
putStrLn "====== Tests Start ======"
putStrLn (printMaybe <b>Nothing</b>)
但不清楚 Maybe a
类型的 a
是什么 Nothing
。你可以给编译器一个提示:
main = do
putStrLn "====== Tests Start ======"
putStrLn (printMaybe (Nothing <b>:: Maybe Int</b>))