【发布时间】:2019-02-21 11:09:03
【问题描述】:
尝试通过foldr函数实现列表计数
lengthList = foldr (\x s -> s + 1) 0
给出以下错误
* Ambiguous type variable `t0' arising from a use of `foldr'
prevents the constraint `(Foldable t0)' from being solved.
Relevant bindings include
lengthList :: t0 a -> Integer (bound at lenListFoldr.hs:2: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 23 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
* In the expression: foldr (\ x s -> s + 1) 0
In an equation for `lengthList':
lengthList = foldr (\ x s -> s + 1) 0
我该如何解决这个问题?
【问题讨论】:
-
我无法重现这个。您是否添加了签名,或者使用此
lengthList拨打了电话? -
我只是用 GHCi 8.4.3 加载一行模块 ~\Desktop\hskl> ghci .\lenListFoldr.hs GHCi,版本 8.4.3:haskell.org/ghc :?寻求帮助 [1 of 1] 编译 Main(lenListFoldr.hs,解释) lenListFoldr.hs:1:14: 错误:* 模糊类型变量
t0' arising from a use offoldr' .... -
length:: (Foldable f, Num a) => f a -> a 你需要明确的是 Foldable
-
@WillemVanOnsem 由于禁用了单态限制,它可以直接在 GHCi 中工作。
-
@duplode: aarrghh...是的:s.
标签: haskell types compiler-errors parametric-polymorphism monomorphism-restriction