SlideShare a Scribd company logo
1 of 25
.NET (F Sharp)
Records, Lists, Sequences, Sets, Maps
Dr. Rajeshree Khande
Chapter - 5
• Records,
• Lists
• Sequences
• Sets
• Maps
Record
• A record is similar to a tuple, except it contains named fields.
• A record is defined using the syntax:
• + means the element must occur one or more times.
• Unlike a tuple, a record is explicitly defined as its own type using the type
keyword, and record fields are defined as a semicolon-separated list.
• In many ways, a record can be thought of as a simple class.
type recordName =
{ [ fieldName : dataType ] + }
type website =
{ Title : string;
Url : string }
Record
• A website record is created by specifying the record's fields as follows:
• F# determines a records type by the name and type of its fields, not the order
that fields are used.
• In above example, while the record above is defined with Title first and Url
second
> let homepage = { Title = "Google"; Url = "http://www.google.com" };;
val homepage : website
Record
• it's perfectly valid to write:.
> { Url = "http://www.microsoft.com/"; Title = "Microsoft Corporation" };;
val it : website = {Title = "Microsoft Corporation";
Url = "http://www.microsoft.com/";}
Record
• To access a record's properties use dot notation:
> let homepage = { Title = "Wikibooks"; Url = "http://www.wikibooks.org/" };;
val homepage : website
> homepage.Title;;
val it : string = "Wikibooks"
> homepage.Url;;
val it : string = "http://www.wikibooks.org/"
Example
Example 1 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 = "F sharp Programming "; Url = "www.google.com" }
let cpage = { Title = "Learn C"; Url = "www.Cprogramming.com/cprogramming/index.htm" }
let fsharppage = { Title = "Learn F#"; Url = "www.wikibooks.com/fsharp/index.htm" }
let csharppage = { Title = "Learn C#"; Url = "www.C#.com/csharp/index.htm" }
Example
Example 1 defines a record type named website. Then it creates some records of type website and prints the
records.
(*printing records *)
(printfn "Home Page: Title: %A n t URL: %A") homepage.Title homepage.Url
(printfn "C Page: Title: %A n t URL: %A") cpage.Title cpage.Url
(printfn "F# Page: Title: %A n t URL: %A") fsharppage.Title fsharppage.Url
(printfn "C# Page: Title: %A n t URL: %A") csharppage.Title csharppage.Url
Slide Title
Output type website =
{Title: string;
Url: string;}
val homepage : website = {Title = "F sharp Programming ";
Url = "www.google.com";}
val cpage : website = {Title = "Learn C";
Url = "www.Cprogramming.com/cprogramming/index.htm";}
val fsharppage : website = {Title = "Learn F#";
Url = "www.wikibooks.com/fsharp/index.htm";}
val csharppage : website = {Title = "Learn C#";
Url = "www.C#.com/csharp/index.htm";}
val it : unit = ()
Example 2:
F# program that uses records
type Animal = { Name:string; Weight:int; Wings:bool }
// Create an instance of Animal named cat.
let cat = { Name="cat"; Weight=12; Wings=false }
// Display the cat record.
printfn "%A" cat
// Display the Name, Weight and Wings properties.
printfn "%A" cat.Name
printfn "%A" cat.Weight
printfn "%A" cat.Wings
// Modify an existing record.
// ... Set name to "dog."
let dog = { cat with Name="dog" }
printfn "%A" dog
let bird = { cat with Name="bird"; Wings=true }
printfn "%A" bird
Example 2:
F# program that uses records
{Name = "cat";
Weight = 12;
Wings = false;}
"cat"
12
false
{Name = "dog";
Weight = 12;
Wings = false;}
{Name = "bird";
Weight = 12;
Wings = true;}
Output
Example 2:
Cloning Records
• Records are immutable types, which means that instances of records cannot be
modified.
• The F# compiler will report an error saying "This field is not mutable.“
• However, records can be cloned conveniently using the clone syntax:
• We must use "with" to copy records, changing values
type ExampleRecord = { size:int; valid:bool
}
let example = { size=10; valid=true }
// This causes an error: cannot be compiled.
example.size <- 20
type ExampleRecord = { size:int; valid:bool
}
let example = { size with 10; valid with
true }
Example 2:
Cloning Records
type coords = { X : float; Y : float }
let setX item newX =
{ item with X = newX
The method setX has the type
coords -> float -> coords.
The with keyword creates a clone of item
and set its X property to newX.}
> let start = { X = 1.0; Y = 2.0 };;
val start : coords
> let finish = setX start 15.5;;
val finish : coords
> start;;
val it : coords = {X = 1.0;
Y = 2.0;}
> finish;;
val it : coords = {X = 15.5;
Y = 2.0;}
Note : the setX creates a copy of the record, it doesn't actually mutate the original record instance.
Example 3 :
Creation of Student Record
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 }
Example 3 :
Creation of Student Record
let printStudent msg st =
printfn "%s: %A" msg st
let main() =
let preRegisteredStudent = getStudent "Ruchita" 10
let postRegisteredStudent = registerStudent preRegisteredStudent
printStudent "Before Registration: " preRegisteredStudent
printStudent "After Registration: " postRegisteredStudent
Example 3 :
Creation of Student Record
Before Registration: : {Name = "Ruchita";
ID = 10;
RegistrationText = null;
IsRegistered = false;}
After Registration: : {Name = "Ruchita";
ID = 10;
RegistrationText = "Registered";
IsRegistered = true;}
type student =
{Name: string;
ID: int;
RegistrationText: string;
IsRegistered: bool;}
val getStudent : name:string -> id:int -> student
val registerStudent : st:student -> student
val printStudent : msg:string -> st:'a -> unit
val main : unit -> unit
val it : unit = ()
>
Pattern Matching Records
open System
type coords = { X : float; Y : float }
let getQuadrant = function
| { X = 0.0; Y = 0.0 } -> "Origin"
| item when item.X >= 0.0 && item.Y >= 0.0 -> "I"
| item when item.X <= 0.0 && item.Y >= 0.0 -> "II"
| item when item.X <= 0.0 && item.Y <= 0.0 -> "III"
| item when item.X >= 0.0 && item.Y <= 0.0 -> "IV"
let testCoords (x, y) =
let item = { X = x; Y = y }
printfn "(%f, %f) is in quadrant %s" x y
(getQuadrant item)
let main() =
testCoords(0.0, 0.0)
testCoords(1.0, 1.0)
testCoords(-1.0, 1.0)
testCoords(-1.0, -1.0)
testCoords(1.0, -1.0)
main()
Lists Creating And Initializing A List
• A list is an ordered, immutable series of elements of the same type.
• To some extent it is equivalent to a linked list data structure.
• F# module, Microsoft.FSharp.Collections.List, has the common operations on lists.
• F# imports this module automatically and makes it accessible to every F# application.
Lists Creating And Initializing A List
• A list is an ordered, immutable series of elements of the same type.
• To some extent it is equivalent to a linked list data structure.
• F# module, Microsoft.FSharp.Collections.List, has the common operations on
lists.
• F# imports this module automatically and makes it accessible to every F#
application.
Creating and Initializing a List
Following are the various ways of creating lists −
1. Using list literals
2. Using cons (::) operator.
3. Using the List.init method of List module.
4. Using some syntactic constructs called List Comprehensions
Creating and Initializing a List
1. Using list literals
• Most straightforward method is specify a semicolon-delimited sequence of
values in square brackets
• All the values in the list must have the same type:
• Example
let numbers = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]
Creating and Initializing a List
2. Using cons (::) operator.
• It is very common to build lists up by prepending or consing a value to an
existing list using the :: operator
• Consing creates a new list, but it reuses nodes from the old list, so consing a list
is an extremely efficient O(1) operation.
• Example :
let list2 = 1::2::3::4::5::6::7::8::9::10::[]
let names = ["aaa"; "bbb"; "ccc"; "ddd"]
Creating and Initializing a List
• Example :
>1 :: 2 :: 3 :: [];;
val it : int list = [1; 2; 3]
• [] denotes an empty list
• The :: operator prepends items to a list, returning a
new list
• This operator does not actually mutate lists, it creates
an entirely new list with the prepended element in
the front
> let x = 1 :: 2 :: 3 :: 4 :: [];;
val x : int list
> let y = 12 :: x;;
val y : int list
> x;;
val it : int list = [1; 2; 3; 4]
> y;;
val it : int list = [12; 1; 2; 3; 4]
Creating and Initializing a List
2. Using List.init
• The List.init method of the List module is often used for creating lists. This
method has the type
• The first argument is the desired length of the new list, and the second
argument is an initializer function which generates items in the list.
val init : int -> (int -> 'T) -> 'T list
Using List.init
2. List.init Example
• > List.init 5 (fun index -> index * 3);;
output
val it : int list = [0; 3; 6; 9; 12]
let list5 = List.init 5 (fun index -> (index, index * index, index * index * index))

More Related Content

What's hot

The Next Great Functional Programming Language
The Next Great Functional Programming LanguageThe Next Great Functional Programming Language
The Next Great Functional Programming LanguageJohn De Goes
 
Search Engine-Building with Lucene and Solr
Search Engine-Building with Lucene and SolrSearch Engine-Building with Lucene and Solr
Search Engine-Building with Lucene and SolrKai Chan
 
Xlab #1: Advantages of functional programming in Java 8
Xlab #1: Advantages of functional programming in Java 8Xlab #1: Advantages of functional programming in Java 8
Xlab #1: Advantages of functional programming in Java 8XSolve
 
Scalding: Reaching Efficient MapReduce
Scalding: Reaching Efficient MapReduceScalding: Reaching Efficient MapReduce
Scalding: Reaching Efficient MapReduceLivePerson
 
Perl v5.26 Features (AmsterdamX.pm)
Perl v5.26 Features (AmsterdamX.pm)Perl v5.26 Features (AmsterdamX.pm)
Perl v5.26 Features (AmsterdamX.pm)brian d foy
 
DBIx::Class introduction - 2010
DBIx::Class introduction - 2010DBIx::Class introduction - 2010
DBIx::Class introduction - 2010leo lapworth
 
Hadoop Interview Questions And Answers Part-2 | Big Data Interview Questions ...
Hadoop Interview Questions And Answers Part-2 | Big Data Interview Questions ...Hadoop Interview Questions And Answers Part-2 | Big Data Interview Questions ...
Hadoop Interview Questions And Answers Part-2 | Big Data Interview Questions ...Simplilearn
 
Practical pig
Practical pigPractical pig
Practical pigtrihug
 

What's hot (17)

Neatly folding-a-tree
Neatly folding-a-treeNeatly folding-a-tree
Neatly folding-a-tree
 
The Next Great Functional Programming Language
The Next Great Functional Programming LanguageThe Next Great Functional Programming Language
The Next Great Functional Programming Language
 
Search Engine-Building with Lucene and Solr
Search Engine-Building with Lucene and SolrSearch Engine-Building with Lucene and Solr
Search Engine-Building with Lucene and Solr
 
SPL Primer
SPL PrimerSPL Primer
SPL Primer
 
Xlab #1: Advantages of functional programming in Java 8
Xlab #1: Advantages of functional programming in Java 8Xlab #1: Advantages of functional programming in Java 8
Xlab #1: Advantages of functional programming in Java 8
 
Linq introduction
Linq introductionLinq introduction
Linq introduction
 
php file uploading
php file uploadingphp file uploading
php file uploading
 
Scalding: Reaching Efficient MapReduce
Scalding: Reaching Efficient MapReduceScalding: Reaching Efficient MapReduce
Scalding: Reaching Efficient MapReduce
 
Perl v5.26 Features (AmsterdamX.pm)
Perl v5.26 Features (AmsterdamX.pm)Perl v5.26 Features (AmsterdamX.pm)
Perl v5.26 Features (AmsterdamX.pm)
 
DBIx::Class introduction - 2010
DBIx::Class introduction - 2010DBIx::Class introduction - 2010
DBIx::Class introduction - 2010
 
Perl
PerlPerl
Perl
 
Unix lab manual
Unix lab manualUnix lab manual
Unix lab manual
 
Php i basic chapter 4
Php i basic chapter 4Php i basic chapter 4
Php i basic chapter 4
 
Save Repository From Save
Save Repository From SaveSave Repository From Save
Save Repository From Save
 
Po sm
Po smPo sm
Po sm
 
Hadoop Interview Questions And Answers Part-2 | Big Data Interview Questions ...
Hadoop Interview Questions And Answers Part-2 | Big Data Interview Questions ...Hadoop Interview Questions And Answers Part-2 | Big Data Interview Questions ...
Hadoop Interview Questions And Answers Part-2 | Big Data Interview Questions ...
 
Practical pig
Practical pigPractical pig
Practical pig
 

Similar to .Net (F # ) Records, lists

Introduction to Python for Plone developers
Introduction to Python for Plone developersIntroduction to Python for Plone developers
Introduction to Python for Plone developersJim Roepcke
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingRasan Samarasinghe
 
Python - Getting to the Essence - Points.com - Dave Park
Python - Getting to the Essence - Points.com - Dave ParkPython - Getting to the Essence - Points.com - Dave Park
Python - Getting to the Essence - Points.com - Dave Parkpointstechgeeks
 
Extreme Swift
Extreme SwiftExtreme Swift
Extreme SwiftMovel
 
Static types on javascript?! Type checking approaches to ensure healthy appli...
Static types on javascript?! Type checking approaches to ensure healthy appli...Static types on javascript?! Type checking approaches to ensure healthy appli...
Static types on javascript?! Type checking approaches to ensure healthy appli...Arthur Puthin
 
PHP Workshop Notes
PHP Workshop NotesPHP Workshop Notes
PHP Workshop NotesPamela Fox
 
Introduction To Python
Introduction To  PythonIntroduction To  Python
Introduction To Pythonshailaja30
 
Lecture 4 - Comm Lab: Web @ ITP
Lecture 4 - Comm Lab: Web @ ITPLecture 4 - Comm Lab: Web @ ITP
Lecture 4 - Comm Lab: Web @ ITPyucefmerhi
 
Intoduction to structure
Intoduction to structureIntoduction to structure
Intoduction to structureUtsav276
 

Similar to .Net (F # ) Records, lists (20)

Introduction to Python for Plone developers
Introduction to Python for Plone developersIntroduction to Python for Plone developers
Introduction to Python for Plone developers
 
C to perl binding
C to perl bindingC to perl binding
C to perl binding
 
Javascript
JavascriptJavascript
Javascript
 
Antlr V3
Antlr V3Antlr V3
Antlr V3
 
U5 SPC.pptx
U5 SPC.pptxU5 SPC.pptx
U5 SPC.pptx
 
U5 SPC.pptx
U5 SPC.pptxU5 SPC.pptx
U5 SPC.pptx
 
Ds lab handouts
Ds lab handoutsDs lab handouts
Ds lab handouts
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programming
 
lab4_php
lab4_phplab4_php
lab4_php
 
lab4_php
lab4_phplab4_php
lab4_php
 
Python - Getting to the Essence - Points.com - Dave Park
Python - Getting to the Essence - Points.com - Dave ParkPython - Getting to the Essence - Points.com - Dave Park
Python - Getting to the Essence - Points.com - Dave Park
 
Extreme Swift
Extreme SwiftExtreme Swift
Extreme Swift
 
Python Basics
Python BasicsPython Basics
Python Basics
 
Static types on javascript?! Type checking approaches to ensure healthy appli...
Static types on javascript?! Type checking approaches to ensure healthy appli...Static types on javascript?! Type checking approaches to ensure healthy appli...
Static types on javascript?! Type checking approaches to ensure healthy appli...
 
Python - Lecture 12
Python - Lecture 12Python - Lecture 12
Python - Lecture 12
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
 
PHP Workshop Notes
PHP Workshop NotesPHP Workshop Notes
PHP Workshop Notes
 
Introduction To Python
Introduction To  PythonIntroduction To  Python
Introduction To Python
 
Lecture 4 - Comm Lab: Web @ ITP
Lecture 4 - Comm Lab: Web @ ITPLecture 4 - Comm Lab: Web @ ITP
Lecture 4 - Comm Lab: Web @ ITP
 
Intoduction to structure
Intoduction to structureIntoduction to structure
Intoduction to structure
 

More from DrRajeshreeKhande

More from DrRajeshreeKhande (20)

.NET F# Inheritance and operator overloading
.NET F# Inheritance and operator overloading.NET F# Inheritance and operator overloading
.NET F# Inheritance and operator overloading
 
Exception Handling in .NET F#
Exception Handling in .NET F#Exception Handling in .NET F#
Exception Handling in .NET F#
 
.NET F# Events
.NET F# Events.NET F# Events
.NET F# Events
 
.NET F# Class constructor
.NET F# Class constructor.NET F# Class constructor
.NET F# Class constructor
 
.NET F# Abstract class interface
.NET F# Abstract class interface.NET F# Abstract class interface
.NET F# Abstract class interface
 
.Net F# Generic class
.Net F# Generic class.Net F# Generic class
.Net F# Generic class
 
F# Console class
F# Console classF# Console class
F# Console class
 
.net F# mutable dictionay
.net F# mutable dictionay.net F# mutable dictionay
.net F# mutable dictionay
 
F sharp lists & dictionary
F sharp   lists &  dictionaryF sharp   lists &  dictionary
F sharp lists & dictionary
 
F# array searching
F#  array searchingF#  array searching
F# array searching
 
Net (f#) array
Net (f#)  arrayNet (f#)  array
Net (f#) array
 
MS Office for Beginners
MS Office for BeginnersMS Office for Beginners
MS Office for Beginners
 
Java Multi-threading programming
Java Multi-threading programmingJava Multi-threading programming
Java Multi-threading programming
 
Java String class
Java String classJava String class
Java String class
 
JAVA AWT components
JAVA AWT componentsJAVA AWT components
JAVA AWT components
 
Dr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWTDr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWT
 
Dr. Rajeshree Khande Java Interactive input
Dr. Rajeshree Khande Java Interactive inputDr. Rajeshree Khande Java Interactive input
Dr. Rajeshree Khande Java Interactive input
 
Dr. Rajeshree Khande :Intoduction to java
Dr. Rajeshree Khande :Intoduction to javaDr. Rajeshree Khande :Intoduction to java
Dr. Rajeshree Khande :Intoduction to java
 
Java Exceptions Handling
Java Exceptions Handling Java Exceptions Handling
Java Exceptions Handling
 
Dr. Rajeshree Khande : Java Basics
Dr. Rajeshree Khande  : Java BasicsDr. Rajeshree Khande  : Java Basics
Dr. Rajeshree Khande : Java Basics
 

Recently uploaded

Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 

Recently uploaded (20)

Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 

.Net (F # ) Records, lists

  • 1. .NET (F Sharp) Records, Lists, Sequences, Sets, Maps Dr. Rajeshree Khande
  • 2. Chapter - 5 • Records, • Lists • Sequences • Sets • Maps
  • 3. Record • A record is similar to a tuple, except it contains named fields. • A record is defined using the syntax: • + means the element must occur one or more times. • Unlike a tuple, a record is explicitly defined as its own type using the type keyword, and record fields are defined as a semicolon-separated list. • In many ways, a record can be thought of as a simple class. type recordName = { [ fieldName : dataType ] + } type website = { Title : string; Url : string }
  • 4. Record • A website record is created by specifying the record's fields as follows: • F# determines a records type by the name and type of its fields, not the order that fields are used. • In above example, while the record above is defined with Title first and Url second > let homepage = { Title = "Google"; Url = "http://www.google.com" };; val homepage : website
  • 5. Record • it's perfectly valid to write:. > { Url = "http://www.microsoft.com/"; Title = "Microsoft Corporation" };; val it : website = {Title = "Microsoft Corporation"; Url = "http://www.microsoft.com/";}
  • 6. Record • To access a record's properties use dot notation: > let homepage = { Title = "Wikibooks"; Url = "http://www.wikibooks.org/" };; val homepage : website > homepage.Title;; val it : string = "Wikibooks" > homepage.Url;; val it : string = "http://www.wikibooks.org/"
  • 7. Example Example 1 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 = "F sharp Programming "; Url = "www.google.com" } let cpage = { Title = "Learn C"; Url = "www.Cprogramming.com/cprogramming/index.htm" } let fsharppage = { Title = "Learn F#"; Url = "www.wikibooks.com/fsharp/index.htm" } let csharppage = { Title = "Learn C#"; Url = "www.C#.com/csharp/index.htm" }
  • 8. Example Example 1 defines a record type named website. Then it creates some records of type website and prints the records. (*printing records *) (printfn "Home Page: Title: %A n t URL: %A") homepage.Title homepage.Url (printfn "C Page: Title: %A n t URL: %A") cpage.Title cpage.Url (printfn "F# Page: Title: %A n t URL: %A") fsharppage.Title fsharppage.Url (printfn "C# Page: Title: %A n t URL: %A") csharppage.Title csharppage.Url
  • 9. Slide Title Output type website = {Title: string; Url: string;} val homepage : website = {Title = "F sharp Programming "; Url = "www.google.com";} val cpage : website = {Title = "Learn C"; Url = "www.Cprogramming.com/cprogramming/index.htm";} val fsharppage : website = {Title = "Learn F#"; Url = "www.wikibooks.com/fsharp/index.htm";} val csharppage : website = {Title = "Learn C#"; Url = "www.C#.com/csharp/index.htm";} val it : unit = ()
  • 10. Example 2: F# program that uses records type Animal = { Name:string; Weight:int; Wings:bool } // Create an instance of Animal named cat. let cat = { Name="cat"; Weight=12; Wings=false } // Display the cat record. printfn "%A" cat // Display the Name, Weight and Wings properties. printfn "%A" cat.Name printfn "%A" cat.Weight printfn "%A" cat.Wings // Modify an existing record. // ... Set name to "dog." let dog = { cat with Name="dog" } printfn "%A" dog let bird = { cat with Name="bird"; Wings=true } printfn "%A" bird
  • 11. Example 2: F# program that uses records {Name = "cat"; Weight = 12; Wings = false;} "cat" 12 false {Name = "dog"; Weight = 12; Wings = false;} {Name = "bird"; Weight = 12; Wings = true;} Output
  • 12. Example 2: Cloning Records • Records are immutable types, which means that instances of records cannot be modified. • The F# compiler will report an error saying "This field is not mutable.“ • However, records can be cloned conveniently using the clone syntax: • We must use "with" to copy records, changing values type ExampleRecord = { size:int; valid:bool } let example = { size=10; valid=true } // This causes an error: cannot be compiled. example.size <- 20 type ExampleRecord = { size:int; valid:bool } let example = { size with 10; valid with true }
  • 13. Example 2: Cloning Records type coords = { X : float; Y : float } let setX item newX = { item with X = newX The method setX has the type coords -> float -> coords. The with keyword creates a clone of item and set its X property to newX.} > let start = { X = 1.0; Y = 2.0 };; val start : coords > let finish = setX start 15.5;; val finish : coords > start;; val it : coords = {X = 1.0; Y = 2.0;} > finish;; val it : coords = {X = 15.5; Y = 2.0;} Note : the setX creates a copy of the record, it doesn't actually mutate the original record instance.
  • 14. Example 3 : Creation of Student Record 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 }
  • 15. Example 3 : Creation of Student Record let printStudent msg st = printfn "%s: %A" msg st let main() = let preRegisteredStudent = getStudent "Ruchita" 10 let postRegisteredStudent = registerStudent preRegisteredStudent printStudent "Before Registration: " preRegisteredStudent printStudent "After Registration: " postRegisteredStudent
  • 16. Example 3 : Creation of Student Record Before Registration: : {Name = "Ruchita"; ID = 10; RegistrationText = null; IsRegistered = false;} After Registration: : {Name = "Ruchita"; ID = 10; RegistrationText = "Registered"; IsRegistered = true;} type student = {Name: string; ID: int; RegistrationText: string; IsRegistered: bool;} val getStudent : name:string -> id:int -> student val registerStudent : st:student -> student val printStudent : msg:string -> st:'a -> unit val main : unit -> unit val it : unit = () >
  • 17. Pattern Matching Records open System type coords = { X : float; Y : float } let getQuadrant = function | { X = 0.0; Y = 0.0 } -> "Origin" | item when item.X >= 0.0 && item.Y >= 0.0 -> "I" | item when item.X <= 0.0 && item.Y >= 0.0 -> "II" | item when item.X <= 0.0 && item.Y <= 0.0 -> "III" | item when item.X >= 0.0 && item.Y <= 0.0 -> "IV" let testCoords (x, y) = let item = { X = x; Y = y } printfn "(%f, %f) is in quadrant %s" x y (getQuadrant item) let main() = testCoords(0.0, 0.0) testCoords(1.0, 1.0) testCoords(-1.0, 1.0) testCoords(-1.0, -1.0) testCoords(1.0, -1.0) main()
  • 18. Lists Creating And Initializing A List • A list is an ordered, immutable series of elements of the same type. • To some extent it is equivalent to a linked list data structure. • F# module, Microsoft.FSharp.Collections.List, has the common operations on lists. • F# imports this module automatically and makes it accessible to every F# application.
  • 19. Lists Creating And Initializing A List • A list is an ordered, immutable series of elements of the same type. • To some extent it is equivalent to a linked list data structure. • F# module, Microsoft.FSharp.Collections.List, has the common operations on lists. • F# imports this module automatically and makes it accessible to every F# application.
  • 20. Creating and Initializing a List Following are the various ways of creating lists − 1. Using list literals 2. Using cons (::) operator. 3. Using the List.init method of List module. 4. Using some syntactic constructs called List Comprehensions
  • 21. Creating and Initializing a List 1. Using list literals • Most straightforward method is specify a semicolon-delimited sequence of values in square brackets • All the values in the list must have the same type: • Example let numbers = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]
  • 22. Creating and Initializing a List 2. Using cons (::) operator. • It is very common to build lists up by prepending or consing a value to an existing list using the :: operator • Consing creates a new list, but it reuses nodes from the old list, so consing a list is an extremely efficient O(1) operation. • Example : let list2 = 1::2::3::4::5::6::7::8::9::10::[] let names = ["aaa"; "bbb"; "ccc"; "ddd"]
  • 23. Creating and Initializing a List • Example : >1 :: 2 :: 3 :: [];; val it : int list = [1; 2; 3] • [] denotes an empty list • The :: operator prepends items to a list, returning a new list • This operator does not actually mutate lists, it creates an entirely new list with the prepended element in the front > let x = 1 :: 2 :: 3 :: 4 :: [];; val x : int list > let y = 12 :: x;; val y : int list > x;; val it : int list = [1; 2; 3; 4] > y;; val it : int list = [12; 1; 2; 3; 4]
  • 24. Creating and Initializing a List 2. Using List.init • The List.init method of the List module is often used for creating lists. This method has the type • The first argument is the desired length of the new list, and the second argument is an initializer function which generates items in the list. val init : int -> (int -> 'T) -> 'T list
  • 25. Using List.init 2. List.init Example • > List.init 5 (fun index -> index * 3);; output val it : int list = [0; 3; 6; 9; 12] let list5 = List.init 5 (fun index -> (index, index * index, index * index * index))