[Haskell] Do the libraries define S' ?

Bernard James POPE bjpop at students.cs.mu.OZ.AU
Thu Jul 8 01:47:08 EDT 2004


On Wed, Jul 07, 2004 at 01:18:54PM +0100, Graham Klyne wrote:
> There's a pattern of higher-order function usage I find myself repeatedly 
> wanting to use, exemplified by the following:
> 
> [[
> -- combineTest :: (Bool->Bool->Bool) -> (a->Bool) -> (a->Bool) -> (a->Bool)
> combineTest :: (b->c->d) -> (a->b) -> (a->c) -> a -> d
> combineTest c t1 t2 = \a -> c (t1 a) (t2 a)
> 
> (.&.) :: (a->Bool) -> (a->Bool) -> (a->Bool)
> (.&.) = combineTest (&&)
> 
> (.|.) :: (a->Bool) -> (a->Bool) -> (a->Bool)
> (.|.) = combineTest (||)
> 
> All this (the recurring requirement, and the fact that S' is a very 
> well-known combinator) leads me to think that maybe there is a version of 
> S' somewhere in the standard Haskell libraries.

I use almost exactly the same thing in my code. And I nearly came
up with the same names as you! (I have .&&. and .||.)

I find them very useful in guards:

   foo x y
     | (this .&&. that) x = ...

I don't believe this kind of abstraction is defined anywhere in the standard
libraries.

Others have noted that you can rewrite it in terms of the Reader monad. 
Perhaps the Boolean specialistation is useful enough to warrant its own 
definition in a standard library, perhaps Data.Bool?

Cheers,
Bernie.


More information about the Haskell mailing list