Search⌘ K

Further List Operations

Learn how to perform various list operations in ReasonML such as appending lists using recursive functions and operators, accessing the first or nth element with pattern matching and standard functions, and iterating over lists with List.iter. This lesson builds foundational skills to manage and manipulate lists effectively in ReasonML.

Appending a List

A list can be appended to another list using the spread operator. Here’s how it works:

Reason
let myList = [5, 6, 7, 8];
Js.log([1, 2, 3, 4, ...myList]);

Console: [1,[2,[3,[4,[5,[6,[7,[8,0]]]]]]]]

However, the code below would not create the correct structure:

Reason
let list1 = [1, 2, 3, 4];
let list2 = [5, 6, 7, 8];
Js.log([list1, ...[list2]]);

Output: [[1,[2,[3,[4,0]]]],[[5,[6,[7,[8,0]]]],0 ...