“Situations often arise where I need to curry the second argument of a function. For example, the divisor in (/). It’s not hard to come up with a function or operator for doing this, as the following code illustrates. But is there a way to do it in F# without defining a new function or operator?”
For instance, one might want to curry the second argument of the divide function (/) in order to produce a curried “divide-by-N” function. This kind of operation is very common in stack-based languages like FORTH, but is not usually needed in imperative languages.
Here is some code that illustrates what I want to do. I decided to use “><” to represent the “flip*” or “swap” operator.
// This code is presented "as-is" and without warranty
// or implied fitness of any kind; use at your own risk.
// "Flip" or "swap" operations:
let flip f a b = f b a
let (><) f a b = f b a
// Sundry tests:
let thisIs20 = ((/) >< 5) 100
let also20 = ((><) (/) 5) 100
let d5'0 = flip (/) 5
let d5'1 = (/) >< 5
let m5'0 = flip (%) 5
let m5'1 = (%) >< 5
let t0 = d5'0 99 // 19
let t1 = d5'1 99 // 19
let t2 = m5'0 99 // 4
let t3 = m5'1 99 // 4
let divBy = (><) (/)
let divBy5 = divBy 5
let shouldBe20 = divBy5 100
I ask again: is there a good native way to do this in F#? If not, it might well worth standardizing this. (And potentially other FORTH-like operators like “over” and “rot,” perhaps with an extensible syntax.)
*Name suggested by “mau” on hubFS.