What is Array.length in Ocaml?

Overview

An array is a data structure that contains data that are related. In Ocaml, the values of an array must be of the same data type.

Syntax

Array.length arr
The syntax for length

Parameters

arr: This is the array whose length we want to get.

Return value

The value returned is an integer that is the number of elements in the array.

Example

(*create some arrays*)
let evenNumbers = [| 2; 4; 6; 8; 10;|];;
let letters = [|'a'; 'm'; 'e'; 'y'|];;
let fruits = [|"banana"; "paw paw"; "apple";|];;
let chosen = [|true|];;
(*get their lengths*)
print_int(Array.length evenNumbers);;
print_string("\n");;
print_int(Array.length letters);;
print_string("\n");;
print_int(Array.length fruits);;
print_string("\n");;
print_int(Array.length chosen);;

Explanation

  • Line 2–5: We create some arrays in Ocaml.
  • Line 8–14: We get the length of the arrays and print it to the console with the length property.

Free Resources