Mutable Data, Lists,
Dictionary
Array
Searching & Multidimensional Array
List Vs Array
Which is faster/efficient
? ?
• The main difference is that arrays are allocated as a
continuous block of memory
• The Array.map function knows the size of the input
array and can perform just a single allocation to get
all memory for the resulting array.
• On the other hand, the List.map function needs to
separately allocate a single "cons cell" for each
element of the input array
List Vs Array
• However, arrays are generally faster for larger data
sets.
• Using lists has other benefits - because they are
immutable
• Also, cloning a list and adding an element to the
front is super efficient
• while doing similar operation with array would be
really slow (copy the whole array).
List Vs Array
Immutable, allows new lists to
share nodes with other lists.
List literals.
Pattern matching.
Supports mapping & folding.
Linear lookup time.
No random access to
elements, just "forward-only"
traversal.
Mutability prevents arrays from sharing
nodes with other elements in an array.
Array literals.
Pattern matching.
Good special locality of reference ensures
efficient lookup time
Constant lookup time
Not resizable.
Multi-dimensional Arrays
• A multidimensional array can be defined as an
Array of an Array.
• In F# a multidimensional array is defined in two
categories
Types
Rectangular
array
Jagged
Array
Jagged Array
• A jagged array is an array of arrays
• Whose elements are array, elements can be different dimensions
and size
• i. e Every row can have a different size
Jagged Array :Syntax
Jagged Array Creation:Example
let jaggedArray : int[][] = Array.zeroCreate 3
jaggedArray.[0] <- Array.init 1 (fun x -> x)
jaggedArray.[1] <- Array.init 2 (fun x -> x)
jaggedArray.[2] <- Array.init 3 (fun x -> x)
Output
val jaggedArray : int [] [] = [|[|0|]; [|0; 1|]; [|0; 1; 2|]|]
Jagged Array :Example
> [| for a in 1 .. 5 do yield [| 1 .. a |] |];;
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Output:
val it : int array array
= [|[|1|];
[|1; 2|];
[|1; 2; 3|];
[|1; 2; 3; 4|];
[|1; 2; 3; 4; 5|]|]
Jagged Array :Example
let jagged = [| for a in 1 .. 5 do yield [| 1 .. a |] |]
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
for arr in jagged do
for col in arr do
printf "%i " col
printfn "";;
Jagged Array :Example
let jagged = [| for a in 1 .. 5 do yield [| 1 .. a |] |]
for arr in jagged do
for col in arr do
printf "%i " col
printfn "";;
val jagged : int array array
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Rectangular Array
• A rectangular is also an
array of arrays
• All row and column
contains same number
of elements
• C, C++,Java support
jagged array but not
rectangular array
Rectangular arrays are indicated by rectangular brackets with a
comma separating them for each new dimension
Rectangular Array
• A rectangular is also an
array of arrays
• All row and column
contains same number
of elements
• C, C++,Java support
jagged array but not
rectangular array
Rectangular Array :Example
let identityMatrix : float[,] = Array2D.zeroCreate 3 3
identityMatrix.[0,0] <- 1.0
identityMatrix.[1,1] <- 1.0
identityMatrix.[2,2] <- 1.0
Output
val identityMatrix : float [,] = [1.0; 0.0; 0.0]
[0.0; 1.0; 0.0]
1.o 0.0 0.0
0.0. 1.0 0.0
0.0 0.0 1.0
Rectangular Array :Example
let grid = Array2D.init<string> 3 3 (fun row col -> sprintf "row: %i,
col: %i" row col);;
Output
> grid;;
val it : string [,]
= [|[|"row: 0, col: 0"; "row: 0, col: 1"; "row: 0, col: 2"|];
[|"row: 1, col: 0"; "row: 1, col: 1"; "row: 1, col: 2"|];
[|"row: 2, col: 0"; "row: 2, col: 1"; "row: 2, col: 2"|]|]
Searching Array
Searching
Array.find
Array.findIndex
• Takes a Boolean function and
• returns the first element for
which the function returns true,
• or raises a
System.Collections.Generic.KeyN
otFoundException if no element
that satisfies the condition is
found.
• Its like Array.find,
• except that it returns the index
of the element instead of the
element itself.
Searching Array
> // Simple Boolean function
let rec isPowerOfTwo x =
if x = 2 then
true
elif x % 2 = 1 then
false
else isPowerOfTwo (x / 2);;
val isPowerOfTwo : int -> bool
> [| 1; 7; 13; 64; 32 |]
|> Array.tryFind isPowerOfTwo;;
val it : int option = Some 64
> [| 1; 7; 13; 64; 32 |]
|> Array.tryFindIndex
isPowerOfTwo;;
val it : int option = Some 3
Assignment
Example uses array indexers to change individual characters of a
character array to implement a primitive form of encryption known
as ROT13.
ROT13 works by simply taking each letter and rotating it 13 places
forward in the alphabet.
This is achieved in the example by converting each letter to an
integer, adding 13, and then converting it back to a character.
Assignment
The output of our simple program is:
Origional = THE QUICK FOX JUMPED OVER THE LAZY DOG
Encrypted
GUR DHVPX SBK WHZCRQ BIRE GUR YNML QBT
Decrypted
THE QUICK FOX JUMPED OVER THE LAZY DOG

F# array searching

  • 1.
  • 2.
    List Vs Array Whichis faster/efficient ? ? • The main difference is that arrays are allocated as a continuous block of memory • The Array.map function knows the size of the input array and can perform just a single allocation to get all memory for the resulting array. • On the other hand, the List.map function needs to separately allocate a single "cons cell" for each element of the input array
  • 3.
    List Vs Array •However, arrays are generally faster for larger data sets. • Using lists has other benefits - because they are immutable • Also, cloning a list and adding an element to the front is super efficient • while doing similar operation with array would be really slow (copy the whole array).
  • 4.
    List Vs Array Immutable,allows new lists to share nodes with other lists. List literals. Pattern matching. Supports mapping & folding. Linear lookup time. No random access to elements, just "forward-only" traversal. Mutability prevents arrays from sharing nodes with other elements in an array. Array literals. Pattern matching. Good special locality of reference ensures efficient lookup time Constant lookup time Not resizable.
  • 5.
    Multi-dimensional Arrays • Amultidimensional array can be defined as an Array of an Array. • In F# a multidimensional array is defined in two categories Types Rectangular array Jagged Array
  • 6.
    Jagged Array • Ajagged array is an array of arrays • Whose elements are array, elements can be different dimensions and size • i. e Every row can have a different size
  • 7.
  • 8.
    Jagged Array Creation:Example letjaggedArray : int[][] = Array.zeroCreate 3 jaggedArray.[0] <- Array.init 1 (fun x -> x) jaggedArray.[1] <- Array.init 2 (fun x -> x) jaggedArray.[2] <- Array.init 3 (fun x -> x) Output val jaggedArray : int [] [] = [|[|0|]; [|0; 1|]; [|0; 1; 2|]|]
  • 9.
    Jagged Array :Example >[| for a in 1 .. 5 do yield [| 1 .. a |] |];; 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 Output: val it : int array array = [|[|1|]; [|1; 2|]; [|1; 2; 3|]; [|1; 2; 3; 4|]; [|1; 2; 3; 4; 5|]|]
  • 10.
    Jagged Array :Example letjagged = [| for a in 1 .. 5 do yield [| 1 .. a |] |] 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 for arr in jagged do for col in arr do printf "%i " col printfn "";;
  • 11.
    Jagged Array :Example letjagged = [| for a in 1 .. 5 do yield [| 1 .. a |] |] for arr in jagged do for col in arr do printf "%i " col printfn "";; val jagged : int array array 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
  • 12.
    Rectangular Array • Arectangular is also an array of arrays • All row and column contains same number of elements • C, C++,Java support jagged array but not rectangular array Rectangular arrays are indicated by rectangular brackets with a comma separating them for each new dimension
  • 13.
    Rectangular Array • Arectangular is also an array of arrays • All row and column contains same number of elements • C, C++,Java support jagged array but not rectangular array
  • 14.
    Rectangular Array :Example letidentityMatrix : float[,] = Array2D.zeroCreate 3 3 identityMatrix.[0,0] <- 1.0 identityMatrix.[1,1] <- 1.0 identityMatrix.[2,2] <- 1.0 Output val identityMatrix : float [,] = [1.0; 0.0; 0.0] [0.0; 1.0; 0.0] 1.o 0.0 0.0 0.0. 1.0 0.0 0.0 0.0 1.0
  • 15.
    Rectangular Array :Example letgrid = Array2D.init<string> 3 3 (fun row col -> sprintf "row: %i, col: %i" row col);; Output > grid;; val it : string [,] = [|[|"row: 0, col: 0"; "row: 0, col: 1"; "row: 0, col: 2"|]; [|"row: 1, col: 0"; "row: 1, col: 1"; "row: 1, col: 2"|]; [|"row: 2, col: 0"; "row: 2, col: 1"; "row: 2, col: 2"|]|]
  • 16.
    Searching Array Searching Array.find Array.findIndex • Takesa Boolean function and • returns the first element for which the function returns true, • or raises a System.Collections.Generic.KeyN otFoundException if no element that satisfies the condition is found. • Its like Array.find, • except that it returns the index of the element instead of the element itself.
  • 17.
    Searching Array > //Simple Boolean function let rec isPowerOfTwo x = if x = 2 then true elif x % 2 = 1 then false else isPowerOfTwo (x / 2);; val isPowerOfTwo : int -> bool > [| 1; 7; 13; 64; 32 |] |> Array.tryFind isPowerOfTwo;; val it : int option = Some 64 > [| 1; 7; 13; 64; 32 |] |> Array.tryFindIndex isPowerOfTwo;; val it : int option = Some 3
  • 18.
    Assignment Example uses arrayindexers to change individual characters of a character array to implement a primitive form of encryption known as ROT13. ROT13 works by simply taking each letter and rotating it 13 places forward in the alphabet. This is achieved in the example by converting each letter to an integer, adding 13, and then converting it back to a character.
  • 19.
    Assignment The output ofour simple program is: Origional = THE QUICK FOX JUMPED OVER THE LAZY DOG Encrypted GUR DHVPX SBK WHZCRQ BIRE GUR YNML QBT Decrypted THE QUICK FOX JUMPED OVER THE LAZY DOG