I'm trying to write a function that flattens nested Lists.
The code:
data NestedList a = Elem a | List [NestedList a]
flatten :: NestedList a -> [a]
flatten (Elem e) = [e]
flatten (List l) = foldMap flatten l
main = do
let c0 = List []
print $ flatten c0
When I try and provide an empty List from main I get a compiler error:
Ambiguous type variable ‘a0’ arising from a use of ‘print’
prevents the constraint ‘(Show a0)’ from being solved.
Probable fix: use a type annotation to specify what ‘a0’ should be.
I am wondering what causes this error and how to fix it.
Any help is much appreciated!
ais inNestedList afor the valueList []. To fix, again as GHC says, just give a type annotation, eglet c0 = (List [] :: NestedList Int)