In this shot, we'll learn how to get the tail of the list (the last items in a list), without iterating through the list, using the List.tl
method in Ocaml.
List.tl[listitems]
The List.tl
method receives the list for which we want to retrieve the last items.
The List.tl
returns the list without the first item in the list.
Let's take a look at the following example:
open Printflet x = List.tl[3; 2; 5;7];;let () = List.iter (printf "%d ") x
Printf
to print the list items later in the code.x
with list items [3;2;5;7]
.List.tl
to get the items of the list without the first item which is 2, 5, 7
. We then print it to the screen using the printf
.