Working on this homework from 2013 for self-learning.
For the following Algebraic Data Type:
data JoinList m a = Empty
| Single m a
| Append m (JoinList m a) (JoinList m a)
deriving (Eq, Show)
I implemented tag
:
tag :: Monoid m => JoinList m a -> m
tag Empty = mempty
tag (Single x _) = x
tag (Append x left right) = x
Example:
*JoinList Data.Monoid> tag (Append (Product 100) (Single (Product 10) "foo") (Single (Product 10) "bar"))
Product {getProduct = 100}
*JoinList Data.Monoid> tag (Empty :: JoinList (Product Integer) String)
Product {getProduct = 1}
*JoinList Data.Monoid> tag (Single (Product 5) "foo")
Product {getProduct = 5}
Originally, I had implemented the Append ...
case as:
tag (Append x left right) = x `mappend` (tag left) `mappend` (tag right)
But, I'm not sure if I can assume that the Monoid m in JustList m x
is already populated.
Please critique my implementation.