Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`flip` arguments of infix application inline

Given a function for infix usage:

let f a b = (a+10, b)
f 4 5
=> (14,5)
4 `f` 5
=> (14,5)

The arguments can be flipped by defining a helper function:

let g = flip f
4 `g` 5
=> (15,4)

Is it possible to do this inline?

4 `flip f` 5
=> parse error on input `f'
4 `(flip f)` 5
=>  parse error on input `('

My use case is Control.Arrow.first. Instead of

(+10) `first` (7,8)
(17,8)

I would prefer a forward-application-style solution like

(7,8) `forwardFirst` (+10)

without needing to write

let forwardFirst = flip first
like image 847
Tobias Hermann Avatar asked Nov 21 '14 09:11

Tobias Hermann


1 Answers

As detailed in the HaskellWiki article on infix operators,

Note that you can only normally do this with a function that takes two arguments. Actually, for a function taking more than two arguments, you can do it but it's not nearly as nice

The way to do this in your case would be something like this:

let f a b = (a+10, b)
let h a b = (f `flip` a) b
let a = 3
let b = 2
f a b = (13,2)
a `h` b = (12,3)
like image 176
shree.pat18 Avatar answered Sep 30 '22 16:09

shree.pat18