Ternary operators in Haskell, an observation (was: [Haskell] Do the libraries define S' ?)
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 --------------------------------------------------------------------
On Mon, Aug 02, 2004 at 10:54:33PM +0200, hjgtuyl@chello.nl wrote:
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.
You might be interested in my BooleanAlgebra class, which replaces the various boolean operators from the prelude with overloaded versions with various useful instances. The really nice thing is the instance for Bool is exactly the same as the prelude version so this can be dropped into existing code without change (modulo the monomorphism restriction). In particular, there is an instance for the boolean algebra of predicates, so you can say f && g directly where f and g are functions which return bool. http://repetae.net/john/recent/out/Boolean.html it also gives you perl-like short circuting, so Just 'a' || Just 'b' -> Just 'a' and so forth... John -- John Meacham - ⑆repetae.net⑆john⑈
At 14:05 02/08/04 -0700, John Meacham wrote:
You might be interested in my BooleanAlgebra class, which replaces the various boolean operators from the prelude with overloaded versions with various useful instances.
The really nice thing is the instance for Bool is exactly the same as the prelude version so this can be dropped into existing code without change (modulo the monomorphism restriction).
Nice! (I've only looked briefly, but the generalization looks neat.) Is this a candidate for the standard hierarchical libraries? (Data.Boolean?) #g ------------ Graham Klyne For email: http://www.ninebynine.org/#Contact
participants (3)
-
Graham Klyne -
hjgtuyl@chello.nl -
John Meacham