Search code examples
f#pointfreepartial-application

F# point free style division


I'm looking for a way to do point-free style division. Point free style works great with most mathematical operators where the order of the arguments doesn't matter as in the case of multiplication, subtraction or addition.

But the problem is with division where I want to do div(x, y) but the order becomes div(y, x). As in this example:

let aList = [2;4;6;8]
let mut = aList |> List.map ((/)2)

The partially applied function becomes (fun x -> 2 / x). Which will yield [1; 0; 0; 0] while I would like it to be (fun x -> x / 2) -> [1; 2; 3; 4].

Is there any easy way to do this in a point free style?


Solution

  • Add a swap operator/function:

    let swap f = fun x y -> f y x
    let aList = [2;4;6;8]
    let mut = aList |> List.map (swap (/) 2)