Ternary operators in Haskell, an observation (was: [Haskell] Do the libraries define S' ?)

hjgtuyl at chello.nl hjgtuyl at chello.nl
Mon Aug 2 16:54:33 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 (||)

The operators can be also defined as follows:
    (.&.) f g x = (f x) && (g x)
    (.|.) f g x = (f x) || (g x)

It is clear from these definitions, that the operators are ternary
operators.

As operators are essentially the same as functions, in Haskell, any number
of parameters can be given in an operator definition.


Henk-Jan van Tuyl


--------------------------------------------------------------------
    Festina Lente
    Hasten Slowly
    Haast U langzaam
    Eile langsam
    Skynd dig langsomt
    Affrettati lentamente
    Spěchej pomalu
    Skynda långsamt
                       Desiderius Erasmus
--------------------------------------------------------------------
-- 
Met vriendelijke groet,
Herzliche Grüße,
Best regards,
Henk-Jan van Tuyl


--------------------------------------------------------------------
   Festina Lente
   Hasten Slowly
   Haast U langzaam
   Eile langsam
   Skynd dig langsomt
   Affrettati lentamente
   Spěchej pomalu
   Skynda långsamt
                      Desiderius Erasmus
--------------------------------------------------------------------


More information about the Haskell mailing list