In OCaml, a list can be divided using the List.split
function. This function takes a list as input and returns two lists, with the first list containing the first n
elements of the input list and the second list containing the remaining elements.
The List.split
function has the following signature:
List.split : 'a list -> int -> 'a list * 'a list
Here is an example usage of the List.split
function:
# let lst = [1; 2; 3; 4; 5] ;;
val lst : int list = [1; 2; 3; 4; 5]
# let (l1, l2) = List.split lst 3 ;;
val l1 : int list = [1; 2; 3]
val l2 : int list = [4; 5]
In this example, the List.split
function is used to divide the list lst
into two lists, with the first three elements in l1
and the remaining elements in l2
.
Asked: 2023-07-11 16:51:34 +0000
Seen: 13 times
Last updated: Jul 11 '23