How to write Semigroup instance for this data type?

前端 未结 2 1202
野性不改
野性不改 2021-01-06 07:21

I\'m trying to do one of the Semigroup exercises in Haskell Book (Chapter 15, \"Monoid, Semigroup\") but I\'m stuck. The following is given:

newtype Combine          


        
2条回答
  •  无人及你
    2021-01-06 08:25

    What they want is probably the addition of functions. For that, type b needs to be a Semigroup :

    import Data.Semigroup
    
    newtype Combine a b =
      Combine { unCombine :: (a -> b) }
    
    instance Semigroup b => Semigroup (Combine a b) where
      (Combine f) <> (Combine g) = Combine (\x -> f x <> g x)
    

提交回复
热议问题