Most of the time, we want to dynamically loop through a list by using the list's length. The best way to do it is by getting the length of the list first. In this shot, we learn to get the length of the list using the List.length method in OCaml.
List.length method?The List.length method gets the number of items in a list.
List.length list
This List.length method receives a list.
The List.length method returns the total number of the items in a list. Let's see an example.
let x = [3;4;5;7;8;9;0];;print_int(List.length x)
Line 1: We define and initialize our list, x.
Line 2: We print the length of the list x using the List.length method in print_int().