How can I bind the second argument in a function but not the first (in an elegant way)?

flip, which produces a new function with the first two arguments inversed, has already been mentioned as a straigtforward solution.

However, it’s worth noting that Haskell defines a nice infix syntax for binary operators.

First of all, it’s simply

sub = (-)

With parentheses around, all operators are – syntactially too – ordinary functions. Now we can curry operators with some special syntax. Binding to the first operand:

addOne = (1 +)

… and to the second

half = (/ 2)

Thus your code becomes

map (-5) [1..5]

Unfortunately, -5 is a number literal, but you get the point. 🙂 Now since we can turn any function into a binary operator by putting backticks around it like in

f x y == x `f` y

we can use this special operator syntax to write

map (`sub` 5) [1..5]


Note: Currying the first argument is common, the second one – as in your case – nicely possible. But: I wouldn’t do that for futher arguments. Haskell functions are written in a style that the common ones to curry are in the front for exactly that reason.

Using some special syntax for further arguments feels much too implicit for me. Just use the lambda and give the variables descriptive names.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)