[Haskell-beginners] Infix operator and precedence
Chaddaï Fouché
chaddai.fouche at gmail.com
Fri Dec 24 18:51:03 CET 2010
On Fri, Dec 24, 2010 at 4:32 PM, Patrick LeBoutillier
<patrick.leboutillier at gmail.com> wrote:
> Hi,
>
> I'm just messing around with infix operators, and I'm trying to
> implement some kind of ternary if operator:
>
> (??) :: Bool -> (Bool -> a) -> a
> (??) = flip ($)
>
> (?:) :: a -> a -> (Bool -> a)
> t ?: f = \b -> if b then t else f
>
> test b = b ?? ("IF" ?: "THEN")
>
> I would like to get rid of the parentheses around the ("IF" ?: "THEN")
> part, but I'm not familiar with the
> use of theinfixr/infixl functions. I'm not sure which one to use in
> this case and how exactly to use it.
There's also the possibility to do the following :
(True ?? t) f = t
(False ?? t) f = f
Then you can do things like that :
> False ?? "Hello"
> $ False ?? "Goodbye"
> $ "Adios"
"Adios"
You can even define (?:) as ($) if you want your own operators.
--
Jedaï
More information about the Beginners
mailing list