Operators as functions

What is an operator, thinking abstractly? It can be seen as a function of one or two arguments that just have a concise name represented by a single symbol or a very few symbols. F# heartily supports this abstraction. For example, take a look at the following expression:

(%) 10 3 = 10 % 3 

Here, on the left-hand side of the equality sign (=), the (%) function is called with the arguments 10 and 3. On the right-hand side of the equality sign (=) just a 10 % 3 expression is present. Evaluating the whole expression in FSI shows its value as true because sub expressions on the left and right of the equality sign (=) are indeed identical.

Furthermore, the equality sign (=) itself is also an operator. Evaluating the equality sign (=) itself in FSI with the following expression (=);; will reveal the following function signature:

('a -> 'a -> bool) when 'a : equality 

The preceding signature means that (=) is simply a function that takes two arguments of generic type 'a supporting equality and returns a bool value.

Note

For the description of F# core operators, refer to Core.Operators Module (F#) (https://msdn.microsoft.com/en-us/library/ee353754.aspx). Those of you who want to define your own operators, which is not a bad thing if done in moderation, I recommend Operator Overloading (F#) (https://msdn.microsoft.com/en-us/library/dd233204.aspx).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset