Discriminated unions and pattern matching in F#

Discriminated unions are similar to enumerations in F#. However, they offer more flexibility. A discriminated union is a set of types where each type can be different whereas each element of an enumeration must be an integer.

Here’s how we can declare a union of months:

type Months = January | February | March | April | May | June

We can then construct a couple of months as follows:

let jan = January
let feb = February
let apr = April
let jun = June

So it’s enough to use the name of the element in the union, we didn’t have to write the fully qualified name “Months.January” like in the case of an enumeration.

Pattern matching is similar like for an enumeration but we don’t need the “default” clause at the end. It’s enough to account for all possible values. Here’s a list of months and an example of pattern matching. You can think of pattern matching like a series of switch-case statements in C# or Java. Here’s an example of a list of months and an iteration that uses the pattern matching function:

let listOfMonths = [jan; feb; apr; jun]

let unionMatch month = 
    match month with
        | January -> printfn "Dark and cold"
        | February -> printfn "Still cold but more sunshine"
        | March -> printfn "Temperature mostly above 0, increasing number of hours with sunlight"
        | April -> printfn "Spring is here"
        | May -> printfn "Good times"
        | June -> printfn "Lots of sunshine"

for m in listOfMonths do
    unionMatch m

The “month” in the unionMatch function is an input parameter which will be matched against each type in the Months discriminated union. The following will be printed after the iteration:

Dark and cold
Still cold but more sunshine
Spring is here
Lots of sunshine

Here’s an example of combining two unions:

type Months = January | February | March | April | May | June
type Day = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday
type Period = Year of int | DayMonth of Months * Day

A Period can be a Year which requires an integer or it can be a DayMonth which requires a tuple with an element from the Months and an element from the Day union.

Here’s how we can declare a Year and a DayMonth:

let year = Year(2017)
let dm = DayMonth(January, Monday)

…and here’s an example of pattern matching:

let periodMatch p =
    match p with 
        | Year year -> printfn "This is a year"
        | DayMonth (m, d) -> printfn "This is a day and a month"

let m1 = periodMatch year
let m2 = periodMatch dm

…which outputs…

This is a year
This is a day and a month

…in the interactive window.

We can give names to the input parameters as follows:

type Months = January | February | March | April | May | June
type Day = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday
type Period = Year of year:int | DayMonth of month:Months * day:Day

let year = Year(year = 2017)
let dm = DayMonth(month = January, day = Monday)

let periodMatch p =
    match p with 
        | Year year -> printfn "This is a year"
        | DayMonth (m, d) -> printfn "This is a day and a month"

Here’s another example of joining two sets of months:

type Months = January | February | March | April | May | June
type MoreMonths = July | August | September | October | November | December
type AllMonths = FirstHalf of Months | SecondHalf of MoreMonths

let matchAllMonths m =
    match m with
        | FirstHalf fh -> printfn "First half of the year"
        | SecondHalf sh -> printfn "Second half of the year"

let jan = FirstHalf(January)
let feb = FirstHalf(April)
let sep = SecondHalf(September)
let dec = SecondHalf(December)

let janMatch = matchAllMonths jan
let sepMatch = matchAllMonths sep

View all F# related articles here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: