Lists
Prepending (::) is O(1); appending (@) and indexing are O(n). Use arrays for random access.
let nums = [1; 2; 3; 4; 5] // immutable singly linked list
let empty: int list = []
let range = [1 .. 10]
let evens = [2 .. 2 .. 20]
let squares = [ for i in 1 .. 5 -> i * i ] // list comprehension: [1; 4; 9; 16; 25]
let pairs = [ for x in 1 .. 3 do
for y in 'a' .. 'b' do
yield (x, y) ]
let filtered = [ for x in 1 .. 10 do if x % 3 = 0 then x ] // [3; 6; 9]
0 :: nums // cons: [0; 1; 2; 3; 4; 5]
[1; 2] @ [3; 4] // append: [1; 2; 3; 4]
List.head nums, List.tail nums, List.length nums
List.item 2 nums // 3 (or nums[2]; O(n))
List.tryHead [], List.tryItem 10 nums // None, None (safe versions)
List.rev nums
List.map ((*) 2) nums // [2; 4; 6; 8; 10]
List.filter (fun x -> x > 2) nums // [3; 4; 5]
List.reduce (+) nums // 15
List.fold (fun acc x -> acc + x) 0 nums // 15
List.sum nums, List.average [1.0; 2.0], List.max nums
List.sumBy (fun x -> x * x) nums // 55
List.sort [3; 1; 2], List.sortDescending [3; 1; 2], List.sortBy abs [-3; 1; -2]
List.distin
…