Instead of passing an argument to the function. you can create a lambda expression in the body of the function. That lambda expression can take the argument. Here is an example: `let (|Calculate|) = fun x -> x * x * 3.14`
Of course, you can specify the data type of the argument. Here is an example: `let (|Calculate|) = fun (x : float) -> x * x * 3.14`
When calling the function, pass the desired value as argument. Here is an example: let (|Calculate|) = fun (x : float) -> x * x * 3.14 let area = (|Calculate|) 25.50 printfn "Area of Circle: %0.04f" area If necessary, the lambda expression can take as many arguments as you want. Here is an example: ```
let (|Calculate|) = fun x y -> x * y
let length = 48.79
let height = 33.63
let area = (|Calculate|) length height
printfn "Rectangle Characteristics"
printfn "----------------------"
printfn "Length: %0.02f" length
printfn "Height: %0.02f" height
printfn "Area: %0.04f" area
```
In the same way, the function can be called in a matching pattern with the right arguments. Here is an example: let (|Calculate|) = fun x y -> x * y let length = 48.79 let height = 33.63 let area = (|Calculate|) length height match (|Calculate|) length height with | _ -> printfn "Rectangle Characteristics" printfn "----------------------" printfn "Length: %0.02f" length printfn "Height: %0.02f" height printfn "Area: %0.04f" area
As you seen and we will see, there are various ways active patterns are used. We saw that you can create a name for a function, define it, and use it like any normal function. Another option is to create various names (one to seven) that you can use in a matching pattern. The formula to follow is: let (| In the parentheses, create one to seven names. Each name must be included inside pipes. Here is an example: let (|Square|Circle|) . . . This time too, the argument(s) is (are) not (a) requirement but you should add at least one. The argument is used to let you choose what name will be used and when. In this case, the argument should be an integer. In the body of the active pattern, you can create a conditional statement or a matching pattern to associate (select) each option based on a certain value. Here is an example: let (|Square|Circle|) x = match x with | 1 -> Square | _ -> Circle // if x = 1 then Square // else Circle You can then use the active pattern in another statement. For example, you can define a function that will take action based on what name is valid. Here is an example: let (|Square|Circle|) x = match x with | 1 -> Square | _ -> Circle let calculateArea shape value = match shape with | Square -> value * value | Circle -> value * value * 3.14 let area = calculateArea 1 26.79 printfn "Area: %0.04f" area This would produce: Area: 717.7041 Press any key to continue . . .
Another way you can use an active pattern is to validate a choice while not caring for another. This is referred to as a partial active pattern. The formula to create it is: let (| Add three pipes in the parentheses. In the left section, add a name. In the right section, use an underscore. This time also, you should add an argument. The argument will be used to validate the lone active pattern. Here is an example: let (|Sphere|_|) x = match x with | x -> Some(x) let calculateArea volume value = match volume with | Sphere -> value * value * 4.00 * 3.14 | _ -> 0.00 let area = calculateArea () 26.79 printfn "Area: %0.04f" area This would produce: Area: 9014.3635 Press any key to continue . . . |