I am a haskell new bee. I can't just wrap my head around what is going on here
data NestedList a=Elem a | List [NestedList a] deriving Show
append::NestedList a->NestedList a->Either String (NestedList a)
append (Elem x) (Elem y) = Right $ List [Elem x, Elem y]
append (_) (Elem _)=Left "Elements are not allowed"
append (Elem _) (_)=Left "Elements are not allowed"
append (List a) (List b)=List(a++b)`
it gives me error
Couldn't match expected type Either String (NestedList a)'
with actual type
NestedList a'
In the return type of a call of List'
In the expression: List (a ++ b)
In an equation for
append':
append (List a) (List b) = List (a ++ b).
But data NestedList a=Elem a | List [NestedList a]
doesn't it mean that NestedList
is of type Elem
or List
of NestedList
and
append::NestedList a->NestedList a->Either String (NestedList a)
that append can return either String
or NestedList
. Now when I do List(a++b)
I am returning List
. It should work isn't it?
My other function flatten
flatten ::NestedList a->[a]
flatten (Elem x)=[x]
flatten (List(x:xs))=flatten(x)++flatten(List xs)
--flatten NestedList (x:xs)=flatten(x)++flatten(List xs)
flatten(List [])=[]
works fine while its input parameter is also NestedList
but ghc is fine with flatten
(List(x:xs))
where List(x:xs)
is also just List
. why doesn't it complain here? Any inputs?