
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. Can anyone shed some light? Thanks a lot, Patrick -- ===================== Patrick LeBoutillier Rosemère, Québec, Canada

On 12/24/10 10:32, Patrick LeBoutillier wrote:
test b = b ?? ("IF" ?: "THEN")
This is right-associative. This binds more tightly in the ?:. Either is sufficent to make your infix work: infixr 1 ?? infixr 1 ?: or infix 1 ?? infix 2 ?: (I picked low numbers 1 and 2 arbitrarily and because C's ?: operator is also rather low precedence. See what happens if you do + and * inside your ternary-if if you use infix numbers like 8 or 9.)

On Fri, Dec 24, 2010 at 4:32 PM, Patrick LeBoutillier
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ï
participants (3)
-
Chaddaï Fouché
-
Isaac Dupree
-
Patrick LeBoutillier