SlideShare a Scribd company logo
.NET (F#) : Array
Dr. Rajeshree Khande
Array Creation
 Arrays are fixed-size, zero-based, mutable collections of
consecutive data elements that are all of the same type
 There are three syntactical ways of creating arrays without
functions −
1. Adding consecutive values between [| and |] and separated
by semicolons.
2. Putting each element on a separate line, in which case the
semicolon is optional.
3. By using sequence expressions.
 Aarray elements are accessed by using a dot operator (.)
and brackets ([ and ])
Array Creation (Method 1 ): using semicolon
separator
Let arr= [| 1; 2; 3; 4; 5; 6 |]
for i in 0 .. arr.Length - 1 do
printf "%d " arr.[i]
Printfn " "
Array Creation (Method 2 ): Without Semincolon
separator
let arr =
[|
1
2
3
|]
for i in 0 .. arr.Length - 1 do
printf "%d " arr.[i]
printfn " "
Array Creation (Method 3 ): using sequence
let arr = [| for i in 1 .. 10 -> i * i |]
Let arr1=[| for i in 1..20->i*i*i |]
for i in 0 .. arr.Length - 1 do
printf "%d " arr.[i]
Printfn " "
Accessing Elements from Array
Using a dot operator (.) and brackets ([ and ]).
Example : array1.[0]
 Using slice notation, which allow you to
specify a subrange of the array.
Accessing Elements from Array : Using slice notation,
// Accesses elements from 0 to 2.
arr.[0..2]
// Accesses elements from the beginning of the array to 2.
arr.[..2] default begin pos that is 0
// Accesses elements from 2 to the end of the array
arr.[2..] specify the begin /start pos but
end is default that till the end of array
With slice notation, a
new copy of the array
is created.
Accessing Elements from Array : Using slice notation,
Let arr1 =[|1;2;3;4;5;6;|]
printf “%d” arr.[0..2]
output : [|1; 2; 3;|]
printf “%d” arr.[..4]
arr.[2..]
Array Types and Modules
Library module
Microsoft.FSharp.Collections.Array
supports operations on one-dimensional
arrays.
Array Types and Modules
Simple Functions
1. Array.get gets an element.
2. Array.length gives the length of an
array.
3. Array.set sets an element to a specified
value.
4. Array.Creat
Array Types and Modules :Simple Function
Example
let arr1 = Array.create 10 “ ” //empty array
for i in 0 .. arr1.Length - 1 do
Array.set arr1 i (i.ToString()) //0,1,3,4…
for i in 0 .. arr1.Length - 1 do
printf "%s " (Array.get arr1 ) //retrieve
Functions That Create Arrays
1. Array.empty
2. Array.create
3. Array.init
4. Array.zeroCreate
5. Array.copy
6. Array.sub
7. Array.append
8. Array.chooe
9. Array.concat
10. Array.filter
11.Array.rev
Functions That Create Arrays
1. Array.empty -- Creates Empty array
2. Array.create ---- Create array with some size &
with default values
3. Array.init : Create an array with size and
inilialised array element with some value ,
generic function/sequence expression
4. Array.zeroCreate : creates an array with 0
inilialization
Functions That Create Arrays
1. Array.empty creates a new empty array
2. Array.create creates an array of a specified size and
sets all the elements
3. Array.init creates an array, given a dimension and a
function to generate the elements.
4. Array.zeroCreate creates an array in which all the
elements are initialized to the zero value for the
array's type. functions.
Functions That Create Arrays :Example
let myArray = Array.empty
printfn "Length of empty array: %d"
myArray.Length
printfn "Array of floats set to 5.0: %A" (Array.create 10 5.0)
printfn "Array of squares: %A" (Array.init 10 (fun index ->
index * index))
let (myZeroArray : float array) = Array.zeroCreate 10
Functions That Create Arrays :Example
>Array.create 5 "Juliet";;
> val it : string [] = [|"Juliet"; "Juliet"; "Juliet"; "Juliet";
"Juliet"|]
Functions That Create Arrays :Example Output
Length of empty array: 0
Area of floats set to 5.0:
[|5.0; 5.0; 5.0; 5.0; 5.0; 5.0; 5.0; 5.0; 5.0; 5.0|]
Array of squares:
[|0; 1; 4; 9; 16; 25; 36; 49; 64; 81|]
Functions That Create Arrays :Example Output
 Array.zeroCreate does allocate the memory, it will
create the array with each item initialized to the
default value,
 whereas Array.init enables you to set the value of
each item.
Functions That Create Arrays :Example Output
 Array.zeroCreate makes array of elements of type's
default value
 while Array.init makes array of elements using a
provided generator function to create each.
 You can always implement semantics of
Array.zeroCreate by using a correspondent generator
function of Array.init
Functions That Create Arrays : Array.copy
Array.copy creates a new array and copies the
elements from an existing array.
copy is a shallow copy, means, if
the element type is a reference
type, only the reference is copied,
not the underlying object
Functions That Create Arrays : Array.copy
open System.Text
let firstArray: StringBuilder array = Array.init 3 (fun index -> new StringBuilder(""))
----[| “ “; “ “; “ “;|]
let secondArray = Array.copy firstArray
// Reset an element of the first array to a new value.
firstArray.[0] <- new StringBuilder("Test1") // [| “Test1 “; “ “; “ ;|]
// Change an element of the first array.
firstArray.[1].Insert(0, "Test2") |> ignore
printfn "%A" firstArray
printfn "%A" secondArray
Array.copy Output
[|Test1; Test2; |] firstArray
[|; Test2; |] -----secondArray
• Test1 appears only in the first array, because the operation of
creating a new element overwrites the reference in firstArray but
does not affect the original reference to an empty string that is still
present in secondArray.
• Test2 appears in both arrays because the Insert operation on the
System.Text.StringBuilder type affects the underlying
System.Text.StringBuilder object, which is referenced in both arrays.
Array.sub function
 Array.sub generates a new array from a subrange of
an array.
 Specify the subrange by providing the starting index
and the length.
 Example
let a1 = [| 0 .. 99 |]
let a2 = Array.sub a1 5 10
printfn "%A" a2
[|5; 6; 7; 8; 9; 10; 11; 12; 13; 14|]
Array.sub function
A1
A2
Let A2= Array.copy A1
Functions That Create Arrays
1. Array.append
2. Array.map
3. Array.mapi
4. Array.filter
5. Array.choose
6. Array.concat
7. Array.rev
Array.append function
 This Method is used to combine two array.
 Example
let arr1 = [| 4; 5; 6; 7; 8; |]
let arr2 = [| 4; 5; 6; 7; 8; |]
printfn " Array3 :%A" (Array.append [| arr1|] [| arr2|])
Array.append function
 Example
let array3 = [| 1; 2; 3; 4|]
let array4 = [| 5 .. 9 |]
printfn "Appended Array:"
let array5 = Array.append array3 array4
printfn "%A" array5
Array.concat function
 Takes a sequence of arrays and combines them into a
single array. The following code demonstrates
Array.concat.
 Example
Array.concat [ [|0..3|] ; [|4|] ]
//output [|0; 1; 2; 3; 4|]
Array.concat [| [|0..3|] ; [|4|] |]
//output [|0; 1; 2; 3; 4|]
What is Difference between
Array.append & Array.concat
?
Array.append Array.concat
Concatenates two array Concatenates list of array
Array.append arr1 arr2 Array.concat [arr1; arr2; arr3;]
Complexity o(n) Complexity o(n + m)
Where
n : length of resulting array
m : length of supplied list
Array.map function
 Takes an array and returns another array of the same
length with the result of applying a function to each
element.
Input A Output B
Length A = Length B
Array.map function
 Takes an array and returns another array of the same
length with the result of applying a function to each
element.
let numbers = [|1..5|]
let squares =
numbers
|> Array.map (fun i -> i * i)
printfn "%A" square
Array.map function
Array.map function
1 2 3 4 5
Fun i -> i * i
1 4 9 16 25
Array.mapi function
 It is similar to map but provide the index of each element.
 Example
let letters = [|'a';'b';'c';'d'|]
letters |> Array.mapi (fun i char -> sprintf "The
letter at index %i is %c" i char)
Array.filter function
 Only returns those elements on which the function
applied returns true.
 Example
let numbers = [|1..20|]
let evenNumbers =
numbers
|> Array.filter (fun n -> n % 2 = 0)
Array.choose function
 Only returns those elements on wich the function
applied returns a ‘Some’ result.
Filters and maps an array, returning a new array
consisting of all elements which returned Some
Note : the element type of the array does not
have to match the type of the value returned in
the option type
Array.choose function
 Example 1:
let numbers = [|1..20|]
let evenNum =
numbers
|> Array.choose (fun n -> if ( n % 2 = 0 ) then
Some(n)
else
None)
printfn “%A” evenNum
Array.choose function
 Example 2:
let numbers = [|1..20|]
let Num =
numbers
|> Array.choose (fun elem -> if elem % 2 = 0 then
Some(float (elem*elem - 1))
else
None)
printfn “%A” Num
Output
[|3.0; 15.0; 35.0; 63.0; 99.0|]
Array.rev function
 Generates a new array by reversing the order of an
existing array.
 Example 2:
let arr1 = "rohatash".ToCharArray()
printfn " Array3 :%A" (Array.rev( arr1))
type OccupancyStatus =
| Other = 0
| Available = 1
| Occupied = 2
| NeedsRepair = 3
| NotReady = 4
type Apartment = {
UnitNumber : string
Bedrooms : int
Bathrooms : float
SecurityDeposit : int
MonthlyRate : int
Status : OccupancyStatus }
let apartments = [|
{ UnitNumber = "101"; Bedrooms = 2; Bathrooms = 2.00; SecurityDeposit = 650; MonthlyRate = 1150; Status =
OccupancyStatus.Available }
{ UnitNumber = "102"; Bedrooms = 1; Bathrooms = 1.00; SecurityDeposit = 500; MonthlyRate = 950; Status =
OccupancyStatus.NeedsRepair }
{ UnitNumber = "103"; Bedrooms = 1; Bathrooms = 1.00; SecurityDeposit = 500; MonthlyRate = 925; Status =
OccupancyStatus.Available }
{ UnitNumber = "104"; Bedrooms = 3; Bathrooms = 2.00; SecurityDeposit = 850; MonthlyRate = 1350; Status =
OccupancyStatus.Occupied }
{ UnitNumber = "105"; Bedrooms = 2; Bathrooms = 1.00; SecurityDeposit = 550; MonthlyRate = 1150; Status =
OccupancyStatus.Available }
{ UnitNumber = "106"; Bedrooms = 3; Bathrooms = 2.00; SecurityDeposit = 1350; MonthlyRate = 850; Status =
OccupancyStatus.Available }
{ UnitNumber = "107"; Bedrooms = 3; Bathrooms = 2.00; SecurityDeposit = 1285; MonthlyRate = 850; Status =
OccupancyStatus.NotReady }
{ UnitNumber = "108"; Bedrooms = 1; Bathrooms = 1.00; SecurityDeposit = 885; MonthlyRate = 500; Status =
OccupancyStatus.Available }
{ UnitNumber = "109"; Bedrooms = 2; Bathrooms = 2.00; SecurityDeposit = 1150; MonthlyRate = 650; Status =
OccupancyStatus.Available }
{ UnitNumber = "110"; Bedrooms = 1; Bathrooms = 1.00; SecurityDeposit = 895; MonthlyRate = 500; Status =
OccupancyStatus.Available }
|]
let reversedOrder = Array.rev apartments

More Related Content

PDF
Python programming : Arrays
Emertxe Information Technologies Pvt Ltd
 
PPTX
Python array
Arnab Chakraborty
 
DOC
Array properties
Shravan Sharma
 
PDF
15 ruby arrays
Walker Maidana
 
PDF
Arrays in python
Lifna C.S
 
PDF
Arrays In Python | Python Array Operations | Edureka
Edureka!
 
PPT
Array in Java
Shehrevar Davierwala
 
PPT
basic concepts
Jetti Chowdary
 
Python programming : Arrays
Emertxe Information Technologies Pvt Ltd
 
Python array
Arnab Chakraborty
 
Array properties
Shravan Sharma
 
15 ruby arrays
Walker Maidana
 
Arrays in python
Lifna C.S
 
Arrays In Python | Python Array Operations | Edureka
Edureka!
 
Array in Java
Shehrevar Davierwala
 
basic concepts
Jetti Chowdary
 

What's hot (18)

PDF
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
PDF
Numpy string functions
TONO KURIAKOSE
 
PPTX
Java String Handling
Infoviaan Technologies
 
PPT
07 Arrays
maznabili
 
PPT
Java: Introduction to Arrays
Tareq Hasan
 
PPT
Strings Arrays
phanleson
 
PPTX
Intro To C++ - Class #18: Vectors & Arrays
Blue Elephant Consulting
 
PPTX
Collection and framework
SARAVANAN GOPALAKRISHNAN
 
PPTX
arrays-120712074248-phpapp01
Abdul Samee
 
PDF
Arrays in python
moazamali28
 
PPTX
C# Arrays
Hock Leng PUAH
 
PPT
Chap07alg
Munkhchimeg
 
PDF
Basic data structures in python
Lifna C.S
 
PPTX
Arrays in java
bhavesh prakash
 
PPTX
Arrays in Java
Abhilash Nair
 
PPT
Chap03alg
Munkhchimeg
 
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
Numpy string functions
TONO KURIAKOSE
 
Java String Handling
Infoviaan Technologies
 
07 Arrays
maznabili
 
Java: Introduction to Arrays
Tareq Hasan
 
Strings Arrays
phanleson
 
Intro To C++ - Class #18: Vectors & Arrays
Blue Elephant Consulting
 
Collection and framework
SARAVANAN GOPALAKRISHNAN
 
arrays-120712074248-phpapp01
Abdul Samee
 
Arrays in python
moazamali28
 
C# Arrays
Hock Leng PUAH
 
Chap07alg
Munkhchimeg
 
Basic data structures in python
Lifna C.S
 
Arrays in java
bhavesh prakash
 
Arrays in Java
Abhilash Nair
 
Chap03alg
Munkhchimeg
 
Ad

Similar to Net (f#) array (20)

PPTX
F# array searching
DrRajeshreeKhande
 
PDF
Unit-5-Part1 Array in Python programming.pdf
582004rohangautam
 
PPTX
ARRAY OPERATIONS.pptx
DarellMuchoko
 
PDF
CP PPT_Unit IV computer programming in c.pdf
saneshgamerz
 
PDF
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
info309708
 
PPTX
object oriented programing in python and pip
LakshmiMarineni
 
PPTX
Arrays in C++
Kashif Nawab
 
PDF
02 arrays
Rajan Gautam
 
PPTX
arrays in c# including Classes handling arrays
JayanthiM19
 
PDF
JavaScript Array Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
Arrays and function basic c programming notes
GOKULKANNANMMECLECTC
 
PPT
Arrays in c programing. practicals and .ppt
Carlos701746
 
PPTX
Java Foundations: Arrays
Svetlin Nakov
 
PPT
Array 31.8.2020 updated
vrgokila
 
PPTX
07. Arrays
Intro C# Book
 
PPTX
16. Arrays Lists Stacks Queues
Intro C# Book
 
PPTX
array is a fundamental data structure that stores a collection of elements.pptx
sshhealthcardrkl
 
PPT
Arrays in Java Programming Language slides
ssuser5d6130
 
PPT
Arrays JavaScript presentacion en power point
DiegoGamboaSafla
 
DOCX
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
pranauvsps
 
F# array searching
DrRajeshreeKhande
 
Unit-5-Part1 Array in Python programming.pdf
582004rohangautam
 
ARRAY OPERATIONS.pptx
DarellMuchoko
 
CP PPT_Unit IV computer programming in c.pdf
saneshgamerz
 
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
info309708
 
object oriented programing in python and pip
LakshmiMarineni
 
Arrays in C++
Kashif Nawab
 
02 arrays
Rajan Gautam
 
arrays in c# including Classes handling arrays
JayanthiM19
 
JavaScript Array Interview Questions PDF By ScholarHat
Scholarhat
 
Arrays and function basic c programming notes
GOKULKANNANMMECLECTC
 
Arrays in c programing. practicals and .ppt
Carlos701746
 
Java Foundations: Arrays
Svetlin Nakov
 
Array 31.8.2020 updated
vrgokila
 
07. Arrays
Intro C# Book
 
16. Arrays Lists Stacks Queues
Intro C# Book
 
array is a fundamental data structure that stores a collection of elements.pptx
sshhealthcardrkl
 
Arrays in Java Programming Language slides
ssuser5d6130
 
Arrays JavaScript presentacion en power point
DiegoGamboaSafla
 
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
pranauvsps
 
Ad

More from DrRajeshreeKhande (19)

PPTX
.NET F# Inheritance and operator overloading
DrRajeshreeKhande
 
PPTX
Exception Handling in .NET F#
DrRajeshreeKhande
 
PPTX
.NET F# Events
DrRajeshreeKhande
 
PPTX
.NET F# Class constructor
DrRajeshreeKhande
 
PPTX
.NET F# Abstract class interface
DrRajeshreeKhande
 
PPTX
.Net F# Generic class
DrRajeshreeKhande
 
PPTX
F# Console class
DrRajeshreeKhande
 
PPTX
.net F# mutable dictionay
DrRajeshreeKhande
 
PPTX
F sharp lists & dictionary
DrRajeshreeKhande
 
PPTX
.Net (F # ) Records, lists
DrRajeshreeKhande
 
PPT
MS Office for Beginners
DrRajeshreeKhande
 
PPSX
Java Multi-threading programming
DrRajeshreeKhande
 
PPSX
Java String class
DrRajeshreeKhande
 
PPTX
JAVA AWT components
DrRajeshreeKhande
 
PPSX
Dr. Rajeshree Khande :Introduction to Java AWT
DrRajeshreeKhande
 
PPSX
Dr. Rajeshree Khande Java Interactive input
DrRajeshreeKhande
 
PPSX
Dr. Rajeshree Khande :Intoduction to java
DrRajeshreeKhande
 
PPSX
Java Exceptions Handling
DrRajeshreeKhande
 
PPSX
Dr. Rajeshree Khande : Java Basics
DrRajeshreeKhande
 
.NET F# Inheritance and operator overloading
DrRajeshreeKhande
 
Exception Handling in .NET F#
DrRajeshreeKhande
 
.NET F# Events
DrRajeshreeKhande
 
.NET F# Class constructor
DrRajeshreeKhande
 
.NET F# Abstract class interface
DrRajeshreeKhande
 
.Net F# Generic class
DrRajeshreeKhande
 
F# Console class
DrRajeshreeKhande
 
.net F# mutable dictionay
DrRajeshreeKhande
 
F sharp lists & dictionary
DrRajeshreeKhande
 
.Net (F # ) Records, lists
DrRajeshreeKhande
 
MS Office for Beginners
DrRajeshreeKhande
 
Java Multi-threading programming
DrRajeshreeKhande
 
Java String class
DrRajeshreeKhande
 
JAVA AWT components
DrRajeshreeKhande
 
Dr. Rajeshree Khande :Introduction to Java AWT
DrRajeshreeKhande
 
Dr. Rajeshree Khande Java Interactive input
DrRajeshreeKhande
 
Dr. Rajeshree Khande :Intoduction to java
DrRajeshreeKhande
 
Java Exceptions Handling
DrRajeshreeKhande
 
Dr. Rajeshree Khande : Java Basics
DrRajeshreeKhande
 

Recently uploaded (20)

PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
mansk2
 
PPTX
Presentation on Janskhiya sthirata kosh.
Ms Usha Vadhel
 
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
TumwineRobert
 
PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PPTX
Understanding operators in c language.pptx
auteharshil95
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
Marta Fijak
 
PDF
Introducing Procurement and Supply L2M1.pdf
labyankof
 
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
Sourav Kr Podder
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
Sourav Kr Podder
 
PPTX
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Week 4 Term 3 Study Techniques revisited.pptx
mansk2
 
Presentation on Janskhiya sthirata kosh.
Ms Usha Vadhel
 
Cardiovascular Pharmacology for pharmacy students.pptx
TumwineRobert
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
Understanding operators in c language.pptx
auteharshil95
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
The Final Stretch: How to Release a Game and Not Die in the Process.
Marta Fijak
 
Introducing Procurement and Supply L2M1.pdf
labyankof
 
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
Open Quiz Monsoon Mind Game Prelims.pptx
Sourav Kr Podder
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
Open Quiz Monsoon Mind Game Final Set.pptx
Sourav Kr Podder
 
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 

Net (f#) array

  • 1. .NET (F#) : Array Dr. Rajeshree Khande
  • 2. Array Creation  Arrays are fixed-size, zero-based, mutable collections of consecutive data elements that are all of the same type  There are three syntactical ways of creating arrays without functions − 1. Adding consecutive values between [| and |] and separated by semicolons. 2. Putting each element on a separate line, in which case the semicolon is optional. 3. By using sequence expressions.  Aarray elements are accessed by using a dot operator (.) and brackets ([ and ])
  • 3. Array Creation (Method 1 ): using semicolon separator Let arr= [| 1; 2; 3; 4; 5; 6 |] for i in 0 .. arr.Length - 1 do printf "%d " arr.[i] Printfn " "
  • 4. Array Creation (Method 2 ): Without Semincolon separator let arr = [| 1 2 3 |] for i in 0 .. arr.Length - 1 do printf "%d " arr.[i] printfn " "
  • 5. Array Creation (Method 3 ): using sequence let arr = [| for i in 1 .. 10 -> i * i |] Let arr1=[| for i in 1..20->i*i*i |] for i in 0 .. arr.Length - 1 do printf "%d " arr.[i] Printfn " "
  • 6. Accessing Elements from Array Using a dot operator (.) and brackets ([ and ]). Example : array1.[0]  Using slice notation, which allow you to specify a subrange of the array.
  • 7. Accessing Elements from Array : Using slice notation, // Accesses elements from 0 to 2. arr.[0..2] // Accesses elements from the beginning of the array to 2. arr.[..2] default begin pos that is 0 // Accesses elements from 2 to the end of the array arr.[2..] specify the begin /start pos but end is default that till the end of array With slice notation, a new copy of the array is created.
  • 8. Accessing Elements from Array : Using slice notation, Let arr1 =[|1;2;3;4;5;6;|] printf “%d” arr.[0..2] output : [|1; 2; 3;|] printf “%d” arr.[..4] arr.[2..]
  • 9. Array Types and Modules Library module Microsoft.FSharp.Collections.Array supports operations on one-dimensional arrays.
  • 10. Array Types and Modules Simple Functions 1. Array.get gets an element. 2. Array.length gives the length of an array. 3. Array.set sets an element to a specified value. 4. Array.Creat
  • 11. Array Types and Modules :Simple Function Example let arr1 = Array.create 10 “ ” //empty array for i in 0 .. arr1.Length - 1 do Array.set arr1 i (i.ToString()) //0,1,3,4… for i in 0 .. arr1.Length - 1 do printf "%s " (Array.get arr1 ) //retrieve
  • 12. Functions That Create Arrays 1. Array.empty 2. Array.create 3. Array.init 4. Array.zeroCreate 5. Array.copy 6. Array.sub 7. Array.append 8. Array.chooe 9. Array.concat 10. Array.filter 11.Array.rev
  • 13. Functions That Create Arrays 1. Array.empty -- Creates Empty array 2. Array.create ---- Create array with some size & with default values 3. Array.init : Create an array with size and inilialised array element with some value , generic function/sequence expression 4. Array.zeroCreate : creates an array with 0 inilialization
  • 14. Functions That Create Arrays 1. Array.empty creates a new empty array 2. Array.create creates an array of a specified size and sets all the elements 3. Array.init creates an array, given a dimension and a function to generate the elements. 4. Array.zeroCreate creates an array in which all the elements are initialized to the zero value for the array's type. functions.
  • 15. Functions That Create Arrays :Example let myArray = Array.empty printfn "Length of empty array: %d" myArray.Length printfn "Array of floats set to 5.0: %A" (Array.create 10 5.0) printfn "Array of squares: %A" (Array.init 10 (fun index -> index * index)) let (myZeroArray : float array) = Array.zeroCreate 10
  • 16. Functions That Create Arrays :Example >Array.create 5 "Juliet";; > val it : string [] = [|"Juliet"; "Juliet"; "Juliet"; "Juliet"; "Juliet"|]
  • 17. Functions That Create Arrays :Example Output Length of empty array: 0 Area of floats set to 5.0: [|5.0; 5.0; 5.0; 5.0; 5.0; 5.0; 5.0; 5.0; 5.0; 5.0|] Array of squares: [|0; 1; 4; 9; 16; 25; 36; 49; 64; 81|]
  • 18. Functions That Create Arrays :Example Output  Array.zeroCreate does allocate the memory, it will create the array with each item initialized to the default value,  whereas Array.init enables you to set the value of each item.
  • 19. Functions That Create Arrays :Example Output  Array.zeroCreate makes array of elements of type's default value  while Array.init makes array of elements using a provided generator function to create each.  You can always implement semantics of Array.zeroCreate by using a correspondent generator function of Array.init
  • 20. Functions That Create Arrays : Array.copy Array.copy creates a new array and copies the elements from an existing array. copy is a shallow copy, means, if the element type is a reference type, only the reference is copied, not the underlying object
  • 21. Functions That Create Arrays : Array.copy open System.Text let firstArray: StringBuilder array = Array.init 3 (fun index -> new StringBuilder("")) ----[| “ “; “ “; “ “;|] let secondArray = Array.copy firstArray // Reset an element of the first array to a new value. firstArray.[0] <- new StringBuilder("Test1") // [| “Test1 “; “ “; “ ;|] // Change an element of the first array. firstArray.[1].Insert(0, "Test2") |> ignore printfn "%A" firstArray printfn "%A" secondArray
  • 22. Array.copy Output [|Test1; Test2; |] firstArray [|; Test2; |] -----secondArray • Test1 appears only in the first array, because the operation of creating a new element overwrites the reference in firstArray but does not affect the original reference to an empty string that is still present in secondArray. • Test2 appears in both arrays because the Insert operation on the System.Text.StringBuilder type affects the underlying System.Text.StringBuilder object, which is referenced in both arrays.
  • 23. Array.sub function  Array.sub generates a new array from a subrange of an array.  Specify the subrange by providing the starting index and the length.  Example let a1 = [| 0 .. 99 |] let a2 = Array.sub a1 5 10 printfn "%A" a2 [|5; 6; 7; 8; 9; 10; 11; 12; 13; 14|]
  • 25. Functions That Create Arrays 1. Array.append 2. Array.map 3. Array.mapi 4. Array.filter 5. Array.choose 6. Array.concat 7. Array.rev
  • 26. Array.append function  This Method is used to combine two array.  Example let arr1 = [| 4; 5; 6; 7; 8; |] let arr2 = [| 4; 5; 6; 7; 8; |] printfn " Array3 :%A" (Array.append [| arr1|] [| arr2|])
  • 27. Array.append function  Example let array3 = [| 1; 2; 3; 4|] let array4 = [| 5 .. 9 |] printfn "Appended Array:" let array5 = Array.append array3 array4 printfn "%A" array5
  • 28. Array.concat function  Takes a sequence of arrays and combines them into a single array. The following code demonstrates Array.concat.  Example Array.concat [ [|0..3|] ; [|4|] ] //output [|0; 1; 2; 3; 4|] Array.concat [| [|0..3|] ; [|4|] |] //output [|0; 1; 2; 3; 4|]
  • 29. What is Difference between Array.append & Array.concat ?
  • 30. Array.append Array.concat Concatenates two array Concatenates list of array Array.append arr1 arr2 Array.concat [arr1; arr2; arr3;] Complexity o(n) Complexity o(n + m) Where n : length of resulting array m : length of supplied list
  • 31. Array.map function  Takes an array and returns another array of the same length with the result of applying a function to each element. Input A Output B Length A = Length B
  • 32. Array.map function  Takes an array and returns another array of the same length with the result of applying a function to each element. let numbers = [|1..5|] let squares = numbers |> Array.map (fun i -> i * i) printfn "%A" square
  • 34. Array.map function 1 2 3 4 5 Fun i -> i * i 1 4 9 16 25
  • 35. Array.mapi function  It is similar to map but provide the index of each element.  Example let letters = [|'a';'b';'c';'d'|] letters |> Array.mapi (fun i char -> sprintf "The letter at index %i is %c" i char)
  • 36. Array.filter function  Only returns those elements on which the function applied returns true.  Example let numbers = [|1..20|] let evenNumbers = numbers |> Array.filter (fun n -> n % 2 = 0)
  • 37. Array.choose function  Only returns those elements on wich the function applied returns a ‘Some’ result. Filters and maps an array, returning a new array consisting of all elements which returned Some Note : the element type of the array does not have to match the type of the value returned in the option type
  • 38. Array.choose function  Example 1: let numbers = [|1..20|] let evenNum = numbers |> Array.choose (fun n -> if ( n % 2 = 0 ) then Some(n) else None) printfn “%A” evenNum
  • 39. Array.choose function  Example 2: let numbers = [|1..20|] let Num = numbers |> Array.choose (fun elem -> if elem % 2 = 0 then Some(float (elem*elem - 1)) else None) printfn “%A” Num Output [|3.0; 15.0; 35.0; 63.0; 99.0|]
  • 40. Array.rev function  Generates a new array by reversing the order of an existing array.  Example 2: let arr1 = "rohatash".ToCharArray() printfn " Array3 :%A" (Array.rev( arr1))
  • 41. type OccupancyStatus = | Other = 0 | Available = 1 | Occupied = 2 | NeedsRepair = 3 | NotReady = 4 type Apartment = { UnitNumber : string Bedrooms : int Bathrooms : float SecurityDeposit : int MonthlyRate : int Status : OccupancyStatus }
  • 42. let apartments = [| { UnitNumber = "101"; Bedrooms = 2; Bathrooms = 2.00; SecurityDeposit = 650; MonthlyRate = 1150; Status = OccupancyStatus.Available } { UnitNumber = "102"; Bedrooms = 1; Bathrooms = 1.00; SecurityDeposit = 500; MonthlyRate = 950; Status = OccupancyStatus.NeedsRepair } { UnitNumber = "103"; Bedrooms = 1; Bathrooms = 1.00; SecurityDeposit = 500; MonthlyRate = 925; Status = OccupancyStatus.Available } { UnitNumber = "104"; Bedrooms = 3; Bathrooms = 2.00; SecurityDeposit = 850; MonthlyRate = 1350; Status = OccupancyStatus.Occupied } { UnitNumber = "105"; Bedrooms = 2; Bathrooms = 1.00; SecurityDeposit = 550; MonthlyRate = 1150; Status = OccupancyStatus.Available } { UnitNumber = "106"; Bedrooms = 3; Bathrooms = 2.00; SecurityDeposit = 1350; MonthlyRate = 850; Status = OccupancyStatus.Available } { UnitNumber = "107"; Bedrooms = 3; Bathrooms = 2.00; SecurityDeposit = 1285; MonthlyRate = 850; Status = OccupancyStatus.NotReady } { UnitNumber = "108"; Bedrooms = 1; Bathrooms = 1.00; SecurityDeposit = 885; MonthlyRate = 500; Status = OccupancyStatus.Available } { UnitNumber = "109"; Bedrooms = 2; Bathrooms = 2.00; SecurityDeposit = 1150; MonthlyRate = 650; Status = OccupancyStatus.Available } { UnitNumber = "110"; Bedrooms = 1; Bathrooms = 1.00; SecurityDeposit = 895; MonthlyRate = 500; Status = OccupancyStatus.Available } |] let reversedOrder = Array.rev apartments