简体   繁体   中英

Instance declaration for datatype MyList to Class Semigroup and Class Monoid

MyList a works like lists I have defined some functions to create concat' the equivalent to (++) . I want to make an instance declaration for MyList a

instance Semigroup [a] where
    -- (<>) :: [a] -> [a] -> [a]
    (<>) = (++)
instance Monoid [a] where
    -- mempty :: [a]
    mempty = []

I have GHCi version 8.2.2.

data MyList a = Leer | Element a (MyList a) deriving (Show,Eq)

last' :: Eq a => MyList a -> a
last' Leer = undefined
last' (Element x(xs))
    | xs == Leer = x
    | otherwise = last' xs
w :: MyList a -> a -> MyList a
w Leer x = Element x (Leer)
w xs x = Element x (xs)
concat' :: Eq a => MyList a -> MyList a -> MyList a
concat' x Leer = x
concat' (Element x (xs)) ys
    | xs == Leer = (Element x (ys))
    | otherwise = concat' (Element x (deletelst xs)) (w ys (last' xs))

length' :: MyList a -> Int
length' Leer = 0
length' (Element x (xs)) = 1 + length' (xs)
take' :: Int -> MyList a -> MyList a
take' _ Leer = Leer
take' 0 _ = Leer
take' z (Element x (xs)) = Element x (take' (z-1) xs)

deletelst :: MyList a-> MyList a
deletelst Leer = Leer
deletelst xs = (take' ((length' xs) - 1) xs) 

instance Semigroup (MyList a) where
    (<>) = concat'
instance Monoid (MyList a) where
    mempty = Leer
    mappend = (<>)

When I try to compile the program this error message pops up:

FH.hs:115:12: error:
    * No instance for (Eq a) arising from a use of concat'
    Possible fix: add (Eq a) to the context of the instance declaration
    * In the expression: concat'
    In an equation for `<>': (<>) = concat'
    In the instance declaration for `Semigroup (MyList a)'

The error message tells you where the problem is:

* In the expression: concat'
In an equation for `<>': (<>) = concat'
In the instance declaration for `Semigroup (MyList a)'

That is here:

instance Semigroup (MyList a) where
    (<>) = concat'

It also tells you what the problem is:

* No instance for (Eq a) arising from a use of concat'
Possible fix: add (Eq a) to the context of the instance declaration

Indeed, you are using concat' to define (<>) , but your definition of concat' requires an instance of Eq a that is not present as a context for the instance declaration of Semigroup (MyList a) —in other words, you are defining instance Semigroup (MyList a) , not instance Eq a => Semigroup (MyList a) . You could add that context, but there is a more principled solution: drop the (unnecessary) Eq a constraint from the definition of concat' (and also last' ) by using pattern matching:

concat' :: MyList a -> MyList a -> MyList a
concat' x Leer              = x
concat' (Element x Leer) ys = Element x ys
concat' (Element x xs) ys   = concat' (Element x (deletelst xs)) (w ys (last' xs))

last' :: MyList a -> a
last' Leer             = undefined
last' (Element x Leer) = x
last' (Element x xs)   = last' xs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM