[Haskell-cafe] A convenient way to deal with conditional function composition?

Nicolas Frisby nicolas.frisby at gmail.com
Tue Apr 10 10:22:54 EDT 2007


Using the Endo newtype can avoid such ambiguities:
  http://darcs.haskell.org/packages/base/Data/Monoid.hs

newtype Endo a = Endo { appEndo :: a -> a }

instance Monoid (Endo a) where
	mempty = Endo id
	Endo f `mappend` Endo g = Endo (f . g)

Endo allows you to explicitly select the monoid behavior of the
endomorphism String -> String instead of using String -> String as an
exponent. It seems 6.4.2 -> 6.6 made a change from a default Monoid
instance for (a -> a) to the more general Monoid instance for (a ->
b).



On 4/10/07, Stefan O'Rear <stefanor at cox.net> wrote:
> On Tue, Apr 10, 2007 at 02:33:41PM +0100, Chris Kuklewicz wrote:
> > Well, since ((.) :: ShowS -> ShowS -> ShowS) is a Monoid, you can use Writer to
> > create the result:
>
> Not portably.
>
> stefan at stefans:~$ ghc-6.4.2 -e '(  ("foo"++) `Data.Monoid.mappend` ("bar"++) ) "END"'
> "foobarEND"
> stefan at stefans:~$ ghc-6.6 -e '(  ("foo"++) `Data.Monoid.mappend` ("bar"++) ) "END"'
> "fooENDbarEND"
>
>
> -- 6.6 sources
> instance Monoid b => Monoid (a -> b) where
>         mempty _ = mempty
>         mappend f g x = f x `mappend` g x
>
>
> Stefan
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list