+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Monoids where
import Data.Monoid
flatten' :: Tree a -> [a]
flatten' = treeFold [] (\l x r -> l ++ [x] ++ r)
+-- monoids
+newtype Sum' a = Sum' a
+ deriving (Eq, Num, Ord, Show)
+
+getSum :: Sum' a -> a
+getSum (Sum' x) = x
+
+instance Num a => Monoid (Sum' a) where
+ mempty = 0
+ mappend = (+)
+
+newtype Prod' a = Prod' a
+ deriving (Eq, Num, Ord, Show)
+
+getProd :: Prod' a -> a
+getProd (Prod' x) = x
+
+instance Num a => Monoid (Prod' a) where
+ mempty = 1
+ mappend = (*)
+
+lst :: [Integer]
+lst = [1,5,8,23,423,99]
+
+prod :: Integer
+prod = getProd $ mconcat $ map Prod' lst