Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ocaml
community creator

What is the List.hd method in Ocaml?

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

In this shot, we'll learn how to get the head of the list (first item in the list) without iterating through the list. We'll use the List.hd method in Ocaml to achieve this.

Syntax

List.hd[listitems]
Syntax for the List.hd method

Parameters

The List.hd method receives the list of which the first item we want to retrieve as a parameter.

Return value

The List.hd method returns the first item in the list.

Example

Let's look at the code below:

let x = [3; 2; 5];;
print_int(List.hd x)
Example of the List.hd method

Explanation

In the code snippet above:

  • Line 1: We create a list x with list items [3;2;5].
  • Line 2: We use the List.hd method to get the head of the list x and then use the print_int() method to print the head item to the console.

RELATED TAGS

ocaml
community creator

CONTRIBUTOR

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring