[Haskell-beginners] Writer and mappend

Britt Anderson britt.uwaterloo at gmail.com
Tue Mar 15 21:18:42 CET 2011


I have a question related to using Writer with a user defined datatype
where I want some of the fields to append and others to just take the
last value

as a reduced example, if I had a data type like this:

data Mydat = Mydat {list1 :: [String], item1 :: Float}

and I wanted a Writer MyDat a

where the strings keep on appending, but the float value just takes
the last value.

would I have to define Float as an instance of Monoid like

instance Monoid Float where
mempty = 0.0
mappend a b = a

?or maybe mappend a b = b

before defining an instance of MyDat

Or can I do something like

instance Monoid MyDat where
mempty = Mydat [] 0
mappend a b = (Mydat (mappend (list1 a) (list1 b)) (item1 a))

without defining an instance for Float?

Thank you, Britt



More information about the Beginners mailing list