Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

pointfree - Partial function application for a non-symmetric operator using point-free style in F#?

How can I create a partial function application for a non-symmetric operator such as the modulus operator with regards to the first argument without any argument names in F#? My first attempt was

let mod10 = (%) 10 which of course translates to

mod10(x) = 10 mod x instead of the desired

mod10(x) = x mod 10. Certainly I could write

let mod10 x = (%)x 10 but I'd like to not have to name the argument so is there some placeholder that can be used, something like

let mod10 = (%)_ 10?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's solution based on functional composition.

let mod10 = (%) >> (|>) 10

UPD Here was a wordy explanation, but programmers speak the code, so I guess the following will describe it much better, in a manner of mathematical proof.

The following expressions are equal:

let m1 x = x % 10                    
let m2 x = (%) x 10                  // x (op) y = (op) x y
let m3 x = ((%) x) 10                // f x y = (f x) y
let m4 x = 10 |>         ((%) x)     // f x = x |> f
let m5 x = ((|>) 10)     ((%) x)     // x |> f = (|>) x f
let m6 x = ((%) x)    |> ((|>) 10)   // f x = x |> f
let m7 x = (x |> (%)) |> ((|>) 10)   // (op) x = x |> (op)
let m8 x = x |> ((%)  >> ((|>) 10))  // f(x) |> g = x |> (f >> g)
let m9   =       (%)  >> ((|>) 10)   // remove formal argument
let m10  =       (%)  >>  (|>) 10    // remove unnecessary parenthesis

Alternative syntax:

let mod10_2 = (|>) 10 << (%)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...