2. 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
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
• 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
6. 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
8. 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|]|]
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
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 "";;
11. 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
12. 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
13. 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
16. 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.
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 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.
19. 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