Does anyone know how to use the partially applied argument infix function (haskell)

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

I would like to be able to apply it in an infix manner so that I can relate it (example below). something like the following, but it really 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 Bool as a second variable? Or something different? I am lost.

PS the reason I wanted to do this was to have an extra append function

+3
source share
5 answers

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

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

(True `f` 1) 3

( ) :

let ft = f True
let ff = f False

1 `ft` 3 `ff` 5

f, :

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

,

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

. .

:

infixl 5 -|
(-|) :: a -> (a -> b) -> b
(-|) = flip ($)

infixl 5 |-
(|-) :: (a -> b) -> a -> b
(|-) = ($)

infixr 5 =|
(|=) :: (a -> b -> c) -> b -> (a -> c)
(|=) = flip

infixr 5 |=
(=|) :: a -> (a -> c) -> c
(=|) = (-|)

f b x y = if b then x else y

main = do
    putStrLn $ "qwe" -| f True |- "asd" -| f False |- "kojo"
    putStrLn $ "qwe" =| f True |= "asd" =| f False |= "kojo"

:

kojo
qwe

-| |- =| |= - .

, , .

+4

infix , , .

if then else, :

infix 0 ?
(?) :: Bool -> (t, t) -> t
c ? (t, e) = if c then t else e
+3

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

. http://www.haskell.org/haskellwiki/Infix_operator#Using_prefix_functions_with_infix_notation.

, - ,

1 `f True` 3 

,

ft = f True
1 `ft` 3

, , , ...

It would seem that there is no “pleasant” and easy way to do this, but I'm sure that there is a higher level that is suitable, or you can do something, for example, zipWithor folds ...

+1
source

Why do you need a boolean function? If the value is never used, why not define f as

f :: a -> a -> a  
f i j  
i `f` j

If a boolean value is really necessary, then as Pvital suggested, create ftrue and ffalse and use them.

0
source

All Articles