Multiple functions applied to a single value

Derek Elkins ddarius at hotpop.com
Thu Nov 27 21:03:45 EST 2003


On Thu, 27 Nov 2003 14:56:03 +0000
Graham Klyne <gk at ninebynine.org> wrote:

(perhaps a more serious and to the point reply later)

> But not all cases I encounter involve lists or monads.  A different
> case might look like this:

Are you sure this doesn't involve monads?

>  >  eval :: (b->c->d) -> (a->b) -> (a->c) -> (a->d)
>  >  eval f g1 g2 a = f (g1 a) (g2 a)

eval :: Monad m => (b -> c -> d) -> m b -> m c -> m d
eval = liftM2
 
> So, for example, a function to test of the two elements of a pair are
> the same might be:
> 
>  > pairSame = eval (==) fst snd
> 
> giving:
> 
>  > pairSame (1,2) -- false
>  > pairSame (3,3) -- true
> 
> 
> Or a function to subtract the second and subsequent elements of a list
> from the first:
> 
>  > firstDiffRest = eval (-) head (sum . tail)
> 
>  > firstDiffRest [10,4,3,2,1] -- 0

(these work fine with a Monad instance ((->) r) which is a Reader monad)



More information about the Haskell-Cafe mailing list