Search code examples
haskellsyntaxoperatorspartial-application

anyone know how to use a partially applied three argument function infix (haskell)


I want to apply a 3 argument function in different ways based on a boolean value (one of the arguments).

I'd like to be able to apply it in an infix manner so I can chain it (example below). something like the following but that actually works.

f :: Bool -> a -> a -> a

f True  i j = i
f False i j = j

... y `(f True)` z `(f False)` b

Do I need to have the Bool as the second variable? Or something else? I'm kind of lost.

P.S. the reason I wanted to do this was to have optional append function


Solution

  • The infix notation for functions with more than two arguments is possible but not very nice. In your case, given

    f :: Bool -> a -> a -> a
    f True i j = i
    f False i j = j
    

    you can write

    (True `f` 1) 3
    

    It's much easier (as suggested by others) to write:

    let ft = f True
    let ff = f False
    

    then you can do

    1 `ft` 3 `ff` 5
    

    If you want to use f you should write:

    (False `f` ((True `f` 1) 3)) 5
    

    You can verify that

    1 `ft` 3 `ff` 5 == (False `f` ((True `f` 1) 3)) 5