I would like to write functions in a certain way. Please consider these 2 functions in pseudo-code (not F #)
F1 = x + y F2 = F1 * 10 // note I did not specify arguments for F1, 'reverse curry' for lack of a better word
What I would like to do for F # is to find out that since
let F1 xy = x + y //val F1 : int -> int -> int
the code let F2 = F1 * 10 would give me the same signature as F1: val F2 : int -> int -> int , and calling F2 2 3 would result in 50: (2 + 3) * 10. That would be pretty cleverly ...
What is happening is completely different. The first line goes as expected:
let F1 xy = x + y //val F1 : int -> int -> int
but when I add the second line let F2 = F1 * 10 , it discards F #. He complains that the type int does not match the type 'a -> 'b -> 'c and F1 now requires member ( + ) .
I could, of course, describe it as follows:
let F1(x, y) = x + y let F2(x, y) = F1(x, y) * 10
But now I could use C #, we are not that far already. Corrupted arguments break a lot of F #'s elegance. Also my real functions F1 and F2 have a lot more arguments than just 2, so that makes me cross, exactly what I wanted to avoid using F #. To say so would be much more natural:
let F1 xy = x + y let F2 = F1 * 10
Is there a way (almost) to do this?
Extra credits: what exactly happens with these error messages? Why does the second line let F2 = F1 * 10 change the typing on the first?
Thanks for your thoughts,
Geert Yang
Update Two jurors who (almost) do what is described.
One using a tuple. The second line looks a little freaky first, works great. A minor flaw I cannot use currying now, or I will have to add even more fancy code.
let F1 (a, b) = a + b let F2 = F1 >> (*) 10 F2(2, 3)
Another approach is to use a record. This is a little more straightforward and easier to get at first glance, but requires more code and ceremony. Removes some of the elegance of F #, more like C #.
type Arg (a, b) = member this.A = a member this.B = b let F1 (a:Arg) = aA + aB let F2 (a:Arg) = F1(a) * 10 F2 (Arg(2, 3))