Trying to implement list counting through foldr function
lengthList = foldr (\x s -> s + 1) 0
gives following error
* 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
How can I fix that?
Add type signature:
Or something similar. The error states: "Probable fix: use a type annotation to specify what `t0' should be." In other words, the compiler could not infer the type. Or, as a comment states: use the function in a context, then the compiler will use the context to infer correct type for
lengthList. I believe the functionfoldruses a class constraintFoldable t; in your case, the compiler doesn't know whatlengthListis folding. By giving the signature above, you boundt0to be a list. Take a look at the output GHCi gives forIn short, GHC can figure out that
ais unused andbis aNum, but it does not knowt.