Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining elements in a list - OCaml

Tags:

ocaml

Is it possible to create a list by combing elements of a list rather than creating a list of lists?

Example:

List.combine ["A";"B"] ["C";"D"];;

I get:

[("A", "C"); ("B", "D")]

Is it possible to generate ["A";"B";"C";"D"] ?

like image 778
JJunior Avatar asked Nov 28 '10 00:11

JJunior


People also ask

How do I split a list into two lists in OCaml?

Just have a function that takes the original list and a empty list. Build up the empty list(with the first half of the original list) until you reach the halfway point and then use the remainder of the original list and the built up empty list to generate your pairs.

Can lists in OCaml be heterogeneous?

Lists in OCaml are homogeneous lists, as opposed to heterogeneous lists in which each element can have a different type.

What does list Fold_left do in OCaml?

So fold_left is "folding in" elements of the list from the left to the right, combining each new element using the operator.

What is :: In OCaml?

:: means 2 camel humps, ' means 1 hump! – Nick Craver. Feb 27, 2010 at 12:09. Ok a decent comment: merd.sourceforge.net/pixel/language-study/… I don't use oCaml, but there's a full syntax list you my find useful, even if there's no real context to it.


1 Answers

I think the @ operator or List.append is what you want.

Example with the @ operator:

# let x = 4::5::[];;
val x : int list = [4; 5]
# let y = 5::6::[];;
val y : int list = [5; 6]
# let z = x@y;;     
val z : int list = [4; 5; 5; 6]
like image 82
Brian Avatar answered Dec 05 '22 04:12

Brian