F# Basic Tutorial
F# Useful Resources
Selected Reading
- F# - Namespaces
- F# - Modules
- F# - Events
- F# - Interfaces
- F# - Inheritance
- F# - Operator Overloading
- F# - Structures
- F# - Classes
- F# - Exception Handling
- F# - Pattern Matching
- F# - Enumerations
- F# - Delegates
- F# - Generics
- F# - Basic I/O
- F# - Mutable Dictionary
- F# - Mutable Lists
- F# - Arrays
- F# - Mutable Data
- F# - Discriminated Unions
- F# - Maps
- F# - Sets
- F# - Sequences
- F# - Lists
- F# - Records
- F# - Tuples
- F# - Options
- F# - Strings
- F# - Functions
- F# - Loops
- F# - Decision Making
- F# - Operators
- F# - Variables
- F# - Data Types
- F# - Basic Syntax
- F# - Program Structure
- F# - Environment Setup
- F# - Overview
- F# - Home
F# Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
F# - Records
F# - Records
A record is similar to a tuple, however it contains named fields. For example,
type website = { title : string; url : string }
Defining Record
A record is defined as a type using the type keyword, and the fields of the record are defined as a semicolon-separated pst.
Syntax for defining a record is −
type recordName = { [ fieldName : dataType ] + }
Creating a Record
You can create a record by specifying the record s fields. For example, let us create a website record named homepage −
let homepage = { Title = "TutorialsPoint"; Url = "www.tutorialspoint.com" }
The following examples will explain the concepts −
Example 1
This program defines a record type named website. Then it creates some records of type website and prints the records.
(* defining a record type named website *) type website = { Title : string; Url : string } (* creating some records *) let homepage = { Title = "TutorialsPoint"; Url = "www.tutorialspoint.com" } let cpage = { Title = "Learn C"; Url = "www.tutorialspoint.com/cprogramming/index.htm" } let fsharppage = { Title = "Learn F#"; Url = "www.tutorialspoint.com/fsharp/index.htm" } let csharppage = { Title = "Learn C#"; Url = "www.tutorialspoint.com/csharp/index.htm" } (*printing records *) (printfn "Home Page: Title: %A URL: %A") homepage.Title homepage.Url (printfn "C Page: Title: %A URL: %A") cpage.Title cpage.Url (printfn "F# Page: Title: %A URL: %A") fsharppage.Title fsharppage.Url (printfn "C# Page: Title: %A URL: %A") csharppage.Title csharppage.Url
When you compile and execute the program, it yields the following output −
Home Page: Title: "TutorialsPoint" URL: "www.tutorialspoint.com" C Page: Title: "Learn C" URL: "www.tutorialspoint.com/cprogramming/index.htm" F# Page: Title: "Learn F#" URL: "www.tutorialspoint.com/fsharp/index.htm" C# Page: Title: "Learn C#" URL: "www.tutorialspoint.com/csharp/index.htm"
Example 2
type student = { Name : string; ID : int; RegistrationText : string; IsRegistered : bool } let getStudent name id = { Name = name; ID = id; RegistrationText = null; IsRegistered = false } let registerStudent st = { st with RegistrationText = "Registered"; IsRegistered = true } let printStudent msg st = printfn "%s: %A" msg st let main() = let preRegisteredStudent = getStudent "Zara" 10 let postRegisteredStudent = registerStudent preRegisteredStudent printStudent "Before Registration: " preRegisteredStudent printStudent "After Registration: " postRegisteredStudent main()
When you compile and execute the program, it yields the following output −
Before Registration: : {Name = "Zara"; ID = 10; RegistrationText = null; IsRegistered = false;} After Registration: : {Name = "Zara"; ID = 10; RegistrationText = "Registered"; IsRegistered = true;}Advertisements