F#

Printfn

let values = [|*5*; *10*; *15*|]
*// Find an element greater than or equal to 9.*
let result = **Array.tryFind** (fun x -> x >= 9) values
*// See if option has a value.*
if result.**IsSome** then do
printfn "Greater or equal to 9: %A" result.Value
*// Find index where value is 15.*
let resultIndex = **Array.tryFindIndex** (fun x -> x = 15) values
*// See if option has a value.*
if resultIndex.**IsSome** then do
*// Print value and get the element.*
printfn "Index with value equal to 15: %A" resultIndex.Value
printfn "%A" (Array.get values resultIndex.Value)Greater or equal to 9: 10
Index with value equal to 15: 2
15

let volumes = [|*20 .. 30*|]
*// Get second element from the array (at index 1).*
let elementTwo = **Array.get** volumes *1*
printfn "Element two: %A" elementTwo
*// Get third element from the array (at index 2).*
printfn "Element three: %A" (**Array.get** volumes *2*)Element two: 21
Element three: 22

let array1 = [|10; 20; 30|]
let array2 = [|40; 50; 60|]
*// Append the second array to the first.*
let merged = **Array.append** array1 array2
*// Print lengths of the arrays.*
printfn *"%d + %d = %d"* array1.Length array2.Length merged.Length
*// Print the merged array.*
printfn "%A" merged3 + 3 = 6
[|10; 20; 30; 40; 50; 60|]

let array1 = [|1; 2; 3|]
let array2 = [|1; 2; 3|]
*// Use allPairs to get all possible pairs from 2 arrays.*
let pairs = **Array.allPairs** array1 array2
printfn "%A" pairs[|(1, 1); (1, 2); (1, 3); (2, 1); (2, 2); (2, 3); (3, 1); (3, 2); (3, 3)|]

Convert

Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.

Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.

This page was last updated on Mar 1, 2023 (edit).