- homogeneous
- immutable
- fast at prepending items
Immutable data structure for sequences of elements that all have the same type. They are processed via pattern matching
let map: ('a => 'b, list('a)) => list('b);
Function map gets as first argument a function getting type a and returning type b. As a second argument it gets a type list
of any type and returns list of any type
List is self-recursive parameterized variant.
type mylist('a) = | Nil; | Cons('a, mylist('a))
let abc = Cons("a", Cons("b", Cons("c", Nil)));
let abc: mylist(string) = Cons("a", Cons("b", Cons("c", Nil)));
In memory list is represented like this:
|"a"| | -> |"b"| | -> |"c"| |
[]
- empty list[head, ...tail]
- Cons pair
let someList = [0, ...myList];
let test =
switch (someList) {
| [] => "Empty"
| [first, ...last] => "Head of the list is " ++ string_of_int(first)
};
let rec len = (myList) =>
switch myList {
| [] => 0
| [_, ...tail] => 1 + len(tail)
};
List.nth(someList, 2);
List.rev(
List.sort(
compare,
[8,6,4,3,3,2,6,8,4]
)
);
[8,6,4,3,3,2,6,8,4] |> List.sort(compare) |> List.rev;
list a syntax sugar.
[1, 2, 3]
is conceptually equivalent toHead(1, Head(2, Head(3, Empty)))
Js.log(Array.of_list(myList));
let rec removeAll = (~value: 'a, l: list('a)) =>
switch l {
| [] => []
| [head, ...tail] when head == value => removeAll(~value, tail)
| [head, ...tail] => [head, ...removeAll(~value, tail)]
};
- map
- for_all
- flatten [[2,4],[2,5],[4,6]] |> List.flatten
- fold_left
[1,2,3] |> List.fold_left((a,b) => a + b, 0);
Array, ArrayLabel For JS compilation (Js.Array)
- mutable
- fast at random access & updates
- fix-sized on native (flexible on JS)
Difference between lists and arrays:
Lists | Arrays | |
---|---|---|
size | small-medium | small-large |
resizable | flexible | fixed |
mutability | immutable | mutable |
elem types | same | same |
access via | destructuring | index |
fastest | prepend/remove first | read / write elems |
- Arrays provide fast indexed access. Lists are processed via recursion and pattern matching.
let myArray = [| "a", "b", "c" |]
ArrayLabels.make(3, "x");
ArrayLabels.init(~f=i=>i, 3);
ArrayLabels.length([| "a", "b", "c" |]);
ArrayLabels to_list
and of_list
- map
- fold_left
let maxOfArray = (arr) =>
ArrayLabels.fold_left(~f=max, ~init=min_int, arr);
Array functions will return arrays
let filterArray = (filter, arr) =>
arr
|> Array.to_list
|> List.filter(filter)
|> Array.of_list;
an Excercise
Determine if a sentence is a Pangram.
Array.map
Array.to_list
List.for_all
let alphabet =
Array.make(26, 0)
|> Array.mapi((i, _) => Char.chr(i + 65))
|> Array.to_list;
let isPangram: string => bool =
s => alphabet |> List.for_all(String.uppercase(s) |> String.contains);