FSharp Functional Programming—Operators
| CSharp-Online.NET:Articles |
| .NET Articles |
| © 2007 Robert Pickering |
Operators
In F#, you can think of operators as a more aesthetically pleasing way to call functions.
F# has two different kinds of operators, prefix and infix; a prefix operator is an operator that takes one operand, and an infix operator takes two or more. Prefix operators appear before their operand, whereas infix operators appear between the first two operands.
F# provides a rich and diverse set of operators that you can use with numeric, Boolean, string, and collection types. The operators defined in F# and its libraries are too numerous to be covered in this section, so instead you’ll look at the way you use and define operators in F# rather than looking at individual operators.
Like in C#, F# operators are overloaded, meaning you can use more than one type with an operator; however, unlike in C#, both operands must be the same type, or the compiler will generate an error. F# also allows users to define and redefine operators; I discuss how to do that at the end of this section.
Operators follow a set of rules similar to C#’s for operator overloading resolution; therefore, any class in the BCL, or any .NET library, that was written to support operator overloading in C# will support it in F#. For example, you can use the + operator to concatenate stings (you can also use ^ for this) as well as to add a System.TimeSpan to a System.DataTime because these types support an overload of the + operator. The following example illustrates this:
#light let ryhm = "Jack " + "and " + "Jill" let anotherRyhm = "Wee " ^ "Willy " ^ "Winky" open System let oneYearLater = DateTime.Now + new TimeSpan(365, 0, 0, 0, 0)
Users can define their own operators or redefine any of the existing ones if they want (although this is not always advisable, because the operators then no longer support overloading). Consider the following perverse example that redefines + to perform subtraction:
#light let (+) a b = a - b print_int (1 + 1)
User-defined (custom) operators must be nonalphanumeric and can be a single character or a group of characters. You can use the following characters in custom operators:
!$%&*+-./<=>?@^|~
:
Custom operators can start with any of the characters on the first line and after that can use any of these characters and also the colon (:), listed on the second line. The syntax for defining an operator is the same as using the let keyword to define a function, except the operator replaces the function name and is surrounded by parentheses so the compiler knows that the symbols are used as a name of an operator rather than as the operator itself. The following example shows defining a custom operator, +:*, that adds its operands and then multiplies them:
#light let ( +:* ) a b = (a + b) * a * b printfn "(1 +:* 2) = %i" (1 +:* 2)
The results of this example, when compiled and executed, are as follows:
(1 +:* 2) = 6
Unary operators always come before the operand; binary operators are always infix.
Operators beginning with "!", "?" and "~" are always prefix. All other operators are post-fix.
|

