Reputation: 3028
I created a datatype that is a list of other elements say
data Tarr = Tarr [Int] deriving (Show)
I would like to concatenate two of these Lists so
Tarr [0,2,4,2] ++ Tarr [1]
but I get an error
<interactive>:43:1:
Couldn't match expected type `[a0]' with actual type `Tarr'
If there was a typeclass for (++) (Concat say) as there is for (==) (Eq) I could implement it as something like
class Concat a where
(+++) :: a -> a -> a
instance Concat Tarr where
(+++) (Tarr a) (Tarr b) = Tarr (a ++ b)
1) How should I solve my problem?
2) Any reason why (++)
isn't defined in a typeclass?
Upvotes: 0
Views: 140
Reputation: 18199
The ++
function applies to lists only, but the <>
/ mappend
function from the Monoid typeclass generalizes it. In fact if you changed your data to a newtype instead, you could do this:
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
import Data.Monoid
newtype Tarr = Tarr [Int] deriving (Show, Monoid)
Upvotes: 5
Reputation: 2489
I don't know why (++)
isn't defined as a typeclass. I, however, would suggest implementing a Monoid
interface for this. I think Monoid
is poorly named, but mappend
and mconcat
seem like well-known functions for concatenating things.
instance Monoid Tarr where
mempty = Tarr []
mappend (Tarr l1) (Tarr l2) = Tarr (l1 ++ l2)
Tarr [1, 2, 3] `mappend` Tarr [4, 5, 6]
Upvotes: 1