Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# Operator/Function Confusion

I'm just getting started on F#, and when playing around with operator overloading, I've run into something I don't quite understand. Now, I understand that you can't use, for example, +* as an overloaded prefix operator; it can only be an infix operator. Here's where I get confused, however:

let (+*) a = a + a * a;;

If I run this, fsi tells me that the function (+*) is an int->int. Great, I can dig that -- it's not an overloaded operator, just a normal function named (+*). So, if I do:

printf "%d" ((+*) 6)

I'll get 42, as I expect to. However, if I try:

printf "%d" (+*) 6
or
printf "%d" (+*)6

It won't compile. I can't put the exact error up right now as I don't have access to an F# compiler at this moment, but why is this? What's going on with the binding here?

like image 288
Perrako Avatar asked Aug 03 '10 00:08

Perrako


1 Answers

It's interpreting this:

printf "%d" (+*) 6

Like this:

printf ("%d") (+*) (6)

In other words, passing three curried arguments to printf, the second of which is a reference to the function +*.

like image 166
munificent Avatar answered Sep 30 '22 14:09

munificent