Ask Your Question
1

How can a list be divided in OCaml?

asked 2023-07-11 16:51:34 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-07-11 17:18:01 +0000

djk gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-11 16:51:34 +0000

Seen: 13 times

Last updated: Jul 11 '23