gpt4 book ai didi

Haskell 使用 foldr 计算长度抛出类型错误

转载 作者:行者123 更新时间:2023-12-01 23:16:10 25 4
gpt4 key购买 nike

尝试通过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 :: [a] -> Int

或类似的东西。错误指出:“可能的修复:使用类型注释来指定‘t0’应该是什么。”换句话说,编译器无法推断类型。或者,如评论所述:在上下文中使用函数,然后编译器将使用上下文为 lengthList 推断正确的类型。我相信函数 foldr 使用类约束 Foldable t;在您的情况下,编译器不知道 lengthList 正在折叠什么。通过上面的签名,您将 t0 绑定(bind)为一个列表。看一下 GHCi 给出的输出

:t foldr
foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b

简而言之,GHC 可以找出a 未被使用,b 是一个Num,但它不知道t

关于Haskell 使用 foldr 计算长度抛出类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52369203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com