"David House" <[EMAIL PROTECTED]> wrote:

> > Or perhaps (?:) or something like that,
> 
> This has come up a few times on #haskell, and the consensus is that a
> tertiary (?:) operator isn't possible because of the deep specialness
> of (:). However, you can simulate it pretty well:
> 
> infixr 1 ?
> (?) :: Bool -> (a, a) -> a
> True  ? (t, _) = t
> False ? (_, t) = t
> 
> length "hello" > 4 ? ("yes it is!", "afraid not")

HaXml has a lifted version of C's tertiary operator, which matches C's
syntax even more closely:

    data ThenElse a = a :> a
    infixr 3 ?>, :>

    (?>) :: (a->Bool) -> ThenElse (a->b) -> (a->b)
    p ?> (f :> g) = \c-> if p c then f c else g c

You can drop it back down to the term level easily enough:

    (?>) :: Bool -> ThenElse a -> a
    p ?> (t :> e) = if p then t else e

Because the operators are right associative, you don't need parens when
you use it:

  length "hello" == 4  ?>  "yes it is!"  :>  "afraid not"

Regards,
    Malcolm
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to