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"]
?
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.
Lists in OCaml are homogeneous lists, as opposed to heterogeneous lists in which each element can have a different type.
So fold_left is "folding in" elements of the list from the left to the right, combining each new element using the operator.
:: 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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With