【发布时间】:2018-01-09 11:26:52
【问题描述】:
我只是尝试在 Haskell 中编写我能想象到的最简单的可能函数,并收到此错误消息。神奇的是,它只在我尝试评估 myHead 以获得空列表时出现。我做错了什么?
module Main
where
myHead :: [a] -> Maybe a
myHead [] = Nothing
myHead (x:_) = Just x
main = do
print (myHead [])
当我从一个文件运行它时,我得到这个输出:
main.hs:15:1: error:
• Ambiguous type variable ‘a0’ arising from a use of ‘print’
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 12 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In a stmt of a 'do' block: print (myHead [])
In the expression: do { print (myHead []) }
In an equation for ‘main’: main = do { print (myHead []) }
<interactive>:3:1: error:
• Variable not in scope: main
• Perhaps you meant ‘min’ (imported from Prelude)
【问题讨论】:
-
如果你在 shell 中运行它,那么你定义了多个
myHead函数。 -
您的标题具有误导性。你的错误在哪里说“非详尽的模式?”请相应更改。