Discriminated Unions In F#
A Discriminated Union (DU) is a feature provided by F# that allows you to define types which can be assigned a fixed set of values, each of which can have different types of data associated with it.
There are many uses of DU, including the modeling of complex data structures in a concise and expressive manner.
Throughout this article, we will take a look at what discriminated unions are, how they are defined, and some examples of how to use them in a F# project.
The following syntax can be used to define discriminatory unions:
type type-name = | case-identifier1 [of [ fieldname1 : ] type1 [ * [ fieldname2 : ] type2 ...] | case-identifier2 [of [fieldname3 : ]type3 [ * [ fieldname4 : ]type4 ...] ...
As an example of how we might implement “choice” with a simple approach, we could describe it as follows:
type choice = | Yes | No
This example illustrates the use of the type choice in the following way:
The following output will be produced as a result:
Voltage State on Choice Type
As an example, let us take a look at the voltage states that set a bit to either high or low using the following code:
Area of Shape Using Choice Type
Using choice type below is an example of calculating the area of a square, circle and rectangle:
And the output will be as follow:
Area of circle with radius 8: 201.061929856 Area of square that has side 12: 144 Area of rectangle with length 3 and width 5 is 15
This F# code defines a discriminated union called Shape with three cases: Circle, Square, and Rectangle. Each case contains different types of data, such as a radius for the Circle case and length and width for the Rectangle case.
The code also defines a function called area that takes a Shape as input and returns the area of that shape. The area function uses pattern matching to handle each case of the Shape union and calculate the area of the corresponding shape.
The code then creates instances of Shape using each case and prints the area of each shape using the area function. The printfn function formats and prints the results to the console.
For example, when the code is run, it creates a Circle with a radius of 8.0 and calculates its area using the area function. The code then prints the result to the console in the format “Area of circle with radius 8.0: <area>”. It does the same for a Square with a side length of 12.0 and a Rectangle with a length of 3.0 and width of 5.0.
This code demonstrates the power and flexibility of discriminated unions in F# for modeling complex data structures and algorithms.
Payment Method using Choice Type
And the output will be:
Credit card Payment method: Purchase cards card ending at: 421215 expiring on: 02/03/2025 PayPal payment method: [email protected] Bank transfer Payment method: account 02230017345 at bank JPMorgan Chase
If you liked this article and found it informative regarding F# functional programming language, you can leave your feedback by reacting below.