I want to create my own list data structure called Nodes. Then I will use my ListConverter class, which contains the 'toList' function, and create an instance of it.
data Nodes a = Empty
| Node a (Nodes a)
class ListConverter a where
toList :: a -> [Integer]
instance (Integral a) => ListConverter (Nodes a) where
toList Empty = []
toList (Node x Empty) = [x]
toList (Node x y) = x : toList y
GHCi tells me, that the expected type is 'Integer' but is currently 'a'. Im very confused, because in the instance I give a type for a (Integral). Here's the error message:
error:
* Couldn't match expected type `Integer' with actual type `a'
`a' is a rigid type variable bound by
the instance declaration
at main.hs:7:10-48
* In the expression: x
In the expression: [x]
In an equation for `toList': toList (Node x Empty) = [x]
* Relevant bindings include
x :: a (bound at main.hs:9:18)
toList :: Nodes a -> [Integer] (bound at main.hs:8:5)
|
9 | toList (Node x Empty) = [x]
| ^
ListConverterto "create an instance of it", where by "it" I believe you mean yourNodestype. If this means that you expecttoListto give you a value of typeNodes a(for somea) then you will be disappointed, because it would take a value of typeNodes aand give a[Integer].