Exercise 6: Types and Discriminated Unions
Like most modern languages, F# provides facilities to create user defined types. F# also allows developers to create “discriminated unions”, which represent data or structures and convey different meanings depending on how they are used.
The following exercise will demonstrate how to create simple types in F#, create discriminated unions and then use those unions in pattern matching algorithms.
Task 1 – Creating a Simple Type in F#
In this task you will create a simple type in F#.
Task 2 – Creating a Discriminated Union
In this task you will see how discriminated unions can be used to hold temperature information. While Celsius, Fahrenheit and Kelvin temperatures are quite different, a discriminated union is able to represent the correct value. Discriminated unions are made up of two pieces of information, the type of the value, known in F# as the constructor, and the value itself.
Task 3 – Using Pattern Matching in Discriminated Unions
In this task, you will see how to use pattern matching to deconstruct discriminated unions.