F# Structures

In this article, we will delve into F# structures and explore their unique features, syntax, and best practices to help you understand and utilize them effectively in your code.

F# Structures are types of data that can be interpreted as values.

This allows you to have a single variable that holds a wide range of data types that are related to each other.

A structure is created by using the struct keyword.



What are F# Structures?

F# structures are lightweight data types that are similar to classes, but with some key differences.

While classes in F# are reference types and are allocated on the heap, structures are value types and are allocated on the stack, making them more memory efficient.

Structures are intended to represent small, immutable data types that do not require complex behavior or inheritance, and are typically used for performance-critical scenarios.


Defining F# Structures

In F#, you can define a structure using the type keyword, followed by the name of the structure, optional type parameters, and a set of structure members, such as properties and methods.

Syntax

Structures can be defined as the following syntax:

[ attributes ]
type [accessibility-modifier] type-name =
   struct
      type-definition-elements
   end
// or
[ attributes ]
[<StructAttribute>]
type [accessibility-modifier] type-name =
   type-definition-elements

There are two different syntaxes that can be used. The first syntax is most commonly used because it allows you to omit the StructAttribute attribute if you use the struct and end keywords.

A structure definition includes the following elements:

  • Definitions and declarations of members.
  • There is a constructor as well as an immutable and mutable field.
  • Implementation of members and interfaces.

There is no inheritance capability for structures, and they aren’t able to contain let or do bindings, unlike classes. There are no let bindings available in structures, so it is required to use the val keyword when declaring fields in structures.

With the val keyword, the value of fields and their types cannot be initialized; instead, they are set to zero or null. The default value should be annotated with DefaultValue whenever there is an implicit constructor included in the structure.


Calculate Length of Line Using Structure

Here is an example of a program that creates a line structure and a constructor for it.

This program uses the following structure to calculate the length of a line:

Example: 

type Line = struct val A1 : float val B1 : float val A2 : float val B2 : floatnew (a1, b1, a2, b2) = {A1 = a1; B1 = b1; A2 = a2; B2 = b2;} end let lenghtCalculation(a : Line)= let sqr a = a * a sqrt(sqr(a.A1 – a.A2) + sqr(a.B1 – a.B2) )let line = new Line(7.0, 8.0, 5.0, 7.0) let length = lenghtCalculation line printfn "Length of the Line is: %g " length

And the output will be:

Length of the Line is: 2.23606797749979

Example Explanation

In this example, we define a structure called Line using the keyword struct. The Line structure has four fields: A1, B1, A2, and B2, each of type float.

We then define a constructor for the Line structure using the new keyword, which takes four parameters: a1, b1, a2, and b2. Inside the constructor, we use record syntax to initialize the Line structure fields with the corresponding parameter values.

Next, we define a function called lengthCalculation which takes a single parameter of type Line. Inside the function, we define a local function sqr that calculates the square of a given number. We then use this sqr function to compute the square of the differences between a.A1 and a.A2, and between a.B1 and a.B2. We add the two squared differences, take the square root of the result using the sqrt function, and return the computed value, which represents the line length.

After defining the lengthCalculation function, we create an instance of the Line structure called line using the new keyword and passing in four values (7.0, 8.0, 5.0, 7.0) as arguments. We then call the lengthCalculation function with line as an argument to compute the length of the line and store the result in a variable called length.

Finally, we use the printfn function to print the calculated length of the line with the message “Length of the Line is: %g ” where %g is a placeholder for the length value, which is then displayed as the final output.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *