The alternative syntax for anonymous function performing matching

F# offers a special syntax to define anonymous functions that perform matching, or pattern matching functions (https://docs.microsoft.com/en-us/dotnet/articles/fsharp/language-reference/match-expressions).

This syntax assumes that the anonymous function has a single parameter that is placed at the beginning of the function body in the invisible match construction. Having this alternative way of defining pattern matching anonymous functions just adds to the language succinctness and also better reflects the intent behind defining such kind of functions within the code.

Continuing with coding exercises, in the latest F# script I will rewrite the validate function using the alternative syntax. However, to achieve this, it is required that you address the following problem. The alternative syntax assumes that the pattern matching function has a single argument, while validate has a pair of arguments. The way out would be to apply skills acquired after reading the previous chapter and performing the currying. The following is the code (Ch4_6.fsx):

open System 
 
let validate key1 key2 = (key1,key2) |> function 
  | ("","") -> "both keys are empty" 
  | (x,y) & (("",_) | (_,"")) -> 
    sprintf "one key is empty: keyA = %s; keyB = %s" x y 
  | (x,y) when x = y -> 
    sprintf "both keys are not empty: keyA = keyB = %s" x 
  | (x,y) -> 
    sprintf "both keys aren't empty: keyA = %s; keyB = %s" x y 
..................Content has been hidden....................

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