Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

list
ocaml
list.length
communitycreator

How to find the length of a list in OCaml

Chinweuba Elijah Azubuike

Overview

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.

What is the List.length method?

The List.length method gets the number of items in a list.

Syntax

List.length list
The syntax of the List.length method

Parameter

This List.length method receives a list.

Return value

The List.length method returns the total number of the items in a list. Let's see an example.

Code

let x = [3;4;5;7;8;9;0];;
print_int(List.length x)
An example showing the usage of the List.length

Explanation

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().

RELATED TAGS

list
ocaml
list.length
communitycreator

CONTRIBUTOR

Chinweuba Elijah Azubuike
RELATED COURSES

View all Courses

Keep Exploring