所以我是 Haskell 的新手,并使用 WikiBooks 学习它。在高阶函数一章中,使用了以下示例。
echoes = foldr (\ x xs -> (replicate x x) ++ xs) []
所以我尝试运行它,但它给了我如下错误:
* Ambiguous type variable `t0' arising from a use of `foldr'
prevents the constraint `(Foldable t0)' from being solved.
Relevant bindings include
echoes :: t0 Int -> [Int] (bound at HavingFun.hs:107:1)
Probable fix: use a type annotation to specify what `t0' should be.
These potential instances exist:
instance Foldable (Either a) -- Defined in `Data.Foldable'
instance Foldable Maybe -- Defined in `Data.Foldable'
instance Foldable ((,) a) -- Defined in `Data.Foldable'
...plus one other
...plus 29 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
* In the expression: foldr (\ x xs -> (replicate x x) ++ xs) []
In an equation for `echoes':
echoes = foldr (\ x xs -> (replicate x x) ++ xs) []
然后,如果我按如下方式编写它,它就可以工作。
echoes lis = foldr (\ x xs -> (replicate x x) ++ xs) [] lis
我对此感到困惑,我认为这在某种程度上与函数的无点定义有关?请澄清这里有什么问题。我正在学习的链接 - https://en.wikibooks.org/wiki/Haskell/Lists_III