![pattern cannot contain anything painted with a pattern pattern cannot contain anything painted with a pattern](https://www.worthingcourtblog.com/wp-content/uploads/2021/02/1-and-feature.jpg)
The list pattern itself can match only lists of a specific number of elements. The list pattern enables lists to be decomposed into a number of elements. | head :: tail -> printf "%d " head printList tail This example uses a cons pattern and a list pattern. The cons pattern is used to decompose a list into the first element, the head, and a list that contains the remaining elements, the tail. The following example is like detectZeroTuple shown in the Tuple Pattern section later in this topic, but here both var1 and var2 are obtained as values by using the AND pattern. The types of both sides of the AND pattern must be compatible. The AND pattern requires that the input match two patterns. The following example demonstrates the OR pattern. The types of both sides of the OR pattern must be compatible.
Pattern cannot contain anything painted with a pattern code#
The OR pattern is used when input data can match multiple patterns, and you want to execute the same code as a result.
![pattern cannot contain anything painted with a pattern pattern cannot contain anything painted with a pattern](https://www.jacksonsart.com/blog/wp-content/uploads/2021/06/Oil_03.jpg)
The following example uses an as pattern. The as clause binds the matched value to a name that can be used in the execution expression of a match expression, or, in the case where this pattern is used in a let binding, the name is added as a binding to the local scope. The as pattern is a pattern that has an as clause appended to it. | (var1, var2) -> printfn "%d equals %d" var1 var2 | (var1, var2) when var1 printfn "%d is less than %d" var1 var2 | (var1, var2) when var1 > var2 -> printfn "%d is greater than %d" var1 var2 The following example demonstrates a variable pattern within a tuple pattern. A variable pattern alone matches any input, but variable patterns often appear within other patterns, therefore enabling more complex structures such as tuples and arrays to be decomposed into variables. The variable pattern assigns the value being matched to a variable name, which is then available for use in the execution expression to the right of the -> symbol. For information about pattern matching in exception handling, see Exceptions: The try.with Expression. The case in which the identifier is an exception is used in pattern matching in the context of exception handlers. For more information about active patterns, see Active Patterns. "Īctive patterns enable you to define more complex custom pattern matching. NameĪny numeric, character, or string literal, an enumeration constant, or a defined literal identifierĪ case value of a discriminated union, an exception label, or an active pattern case At run time, the input is tested against each of the following patterns in the order listed in the table, and patterns are applied recursively, from first to last as they appear in your code, and from left to right for the patterns on each line. Supported patterns are shown in the following table. The optional when condition part is explained in Match Expressions. If a match is not found, the next pattern rule is tested. If a match is found, the result expression is executed. In the match expression, each pattern is examined in turn to see if the input data is compatible with the pattern. | pattern -> result-expressionĮach pattern acts as a rule for transforming input in some way. For more information, see Match Expressions, let Bindings, Lambda Expressions: The fun Keyword, and Exceptions: The try.with Expression.įor example, in the match expression, the pattern is what follows the pipe symbol. They are used when you are processing arguments for functions in let bindings, lambda expressions, and in the exception handlers associated with the try.with expression. Patterns are used in many language constructs, such as the match expression. They are used throughout F# to compare data with a logical structure or structures, decompose data into constituent parts, or extract information from data in various ways. Patterns are rules for transforming input data.