Click here to Skip to main content
Licence CPOL
First Posted 19 Dec 2011
Views 6,156
Bookmarked 1 time

F# Pattern Matching

By | 19 Dec 2011 | Technical Blog
Pattern matching is logically similar to a switch statement in C#, Java, C++, etc; but itz much more powerful. A pattern match is a series of rules that will execute if the pattern matches the input. The pattern match expression then returns the result of the rule that was matched. So, all rules
A Technical Blog article. View original blog here.[^]

Pattern matching is logically similar to a switch statement in C#, Java, C++, etc; but itz much more powerful. A pattern match is a series of rules that will execute if the pattern matches the input. The pattern match expression then returns the result of the rule that was matched. So, all rules in a pattern match must return the same type.

In F#, you use match and with keywords with a series of pattern rules, followed by an arrow -> for pattern matching. Here's a sample F# pattern match code for Odd validation.

> let isOdd x = (x % 2 = 1)
let describeNumber x =
 match isOdd x with
 | true -> printfn "number is odd"
| false -> printfn " number is even";;

> describeNumber 4;;
number is even
val it : unit = ()

Truth Table

Let's take AND truth table example using type inference on pattern matches. Here's the code:

> let testAND x y =
 match x, y with
 | true, true -> true
 | true, false -> false
 | false, true -> false
 | false, false -> true
> testAND true false;;
val it: bool = true

Match Failure

If no match is found during a pattern matching, an exception of type Microsoft.Fsharp.Core.MatchFailureException is raised. You can avoid this by making sure that all possible cases are coverd. Fortunately, F# compiler will issue a warning when it can determine that pattern match rules are incomplete.

> let testAND x y =
 match x, y with
 | true, true -> true
// Commented | true, false -> false 
| false, true -> false
 | false, false -> true
> testAND true false;;

Microsoft.Fsharp.Core.MatchFailureExcption: The match cases were incomplete at:....
Stopped due to error

Pattern matching rules are checked in the order they are declared.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

GanesanSenthilvel

Architect

India India

Member

Currently working as IT Architect for Financial Services applications. Out of 16+ years, spent six career years at major IT firms in USA. Basically from C, C++, VC++, C# family. Loves driving, spending time with friends&family, building my farm house.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 19 Dec 2011
Article Copyright 2011 by GanesanSenthilvel
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid