How to write Semigroup instance for this data type?

前端 未结 2 1180
野性不改
野性不改 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:04

    You can shadow Monoid's (<>) this way:

    import Data.Monoid hiding ((<>))
    

    Then when you import Data.Semigroup, you will have just one (<>) in scope: the one from Data.Semigroup.

    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题