这是我定义的函数任何”当我尝试在Haskell中编写本地函数的类型定义时,为什么会出现此错误?
any' :: (t -> Bool) -> [t] -> Bool
any' f = foldl' step False
where step :: Bool -> t -> Bool
step b x | f x = True
| otherwise = b
我拥抱时加载此错误:
ERROR "folds.hs":65 - Inferred type is not general enough
*** Expression : step
*** Expected type : Bool -> a -> Bool
*** Inferred type : Bool -> _7 -> Bool
...这ghci的中:
folds.hs:65:27:
Couldn't match expected type `t' with actual type `t1'
`t' is a rigid type variable bound by
the type signature for any' :: (t -> Bool) -> [t] -> Bool
at folds.hs:62:9
`t1' is a rigid type variable bound by
the type signature for step :: Bool -> t1 -> Bool at folds.hs:64:22
In the first argument of `f', namely `x'
In the expression: f x
In a stmt of a pattern guard for
an equation for `step':
f x
当我删除步骤的类型定义它工作正常,所以我的问题是... 有一种方法来正确编写该类型的定义,或者我正在处理其中一个无法明确输入本地函数的地方?
您需要的作用域类型变量扩展,否则编译器看到第二个T作为另一个新的多态类型的变量,而不是相同的T作为主要的类型声明。 –
顺便说一句,为什么foldl'而不foldr? –
你是对的Sassa NF,foldr是更好的,因为允许甚至“任何”的东西(重复2)“ – matiascelasco