I'm using a data type to emulate the Heterogeneous List:
data Nested a = Elem a | List [Nested a]
deriving (Show)
So that I can express data like List [Elem 1, Elem 2, List [Elem 3, Elem 4], List [Elem 5], Elem 6]
.
Here I'm writing a function to act like the normal append function to concatenate my heterogeneous lists:
appendn :: Nested a -> Nested a -> Either String (Nested a)
appendn (Elem a) (List x) = Left "error: wrong argument"
appendn (List x) (Elem a) = Left "error: wrong argument"
appendn (List []) (List x) = Right (List x)
appendn (List (x:xs)) (List y) = Right (List (x:appendn (List xs) (List y))
But I'm always getting a parse error at the last row. I'm wondering whether I can use :
operator here to append List
. Could someone help? Thanks.