tuple component functions

Wolfgang Jeltsch wolfgang@jeltsch.net
Fri, 3 Jan 2003 20:08:25 +0100


Hi!

> [...]

> > twin :: a -> (a,a)
> > twin x =3D (x,x)

By using the Monad instance of ((->) a), defined in Control.Monad.Reader,=
 one=20
can write join (,) for twin. (And, by the way, one can use join for funct=
ions=20
in several other useful ways. For example, one can write join (*) for a=20
squaring function. Here one can see how practical it is to implement rath=
er=20
abstract concepts like monads via classes and define appropriate instance=
s of=20
the respective classes.)

> > applyFst :: (a -> b) -> (a, c) -> (b, c)
> > applyFst f =3D applyPair (f . fst, snd)
> >
> > applySnd :: (a -> b) -> (c, a) -> (c, b)
> > applySnd f =3D applyPair (fst, f . snd)

These two can be written as first and second by using the Arrow instance =
of=20
(->). I suppose that other functions you defined can be easily=20
defined/replaced by arrow expressions as well.

> [...]

Wolfgang