Skip to content

Commit

Permalink
Add an iter function to LinkedList
Browse files Browse the repository at this point in the history
  • Loading branch information
jbranchaud committed Jul 11, 2018
1 parent 7eda798 commit f835239
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Index.re
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ module LinkedList = {
| _ => raise(Invalid_argument("Length below 0"))
};

let rec iter = (fn: 'a => unit, linked_list) =>
switch (linked_list) {
| Empty => (() => ())
| Node(head, rest) =>
fn(head);
iter(fn, rest);
};

let data = linked_list =>
switch (linked_list) {
| Empty => raise(Not_found)
Expand Down Expand Up @@ -47,5 +55,8 @@ print_endline(string_of_int(LinkedList.data(next_node)));
let next_node_2 = LinkedList.next(next_node);
print_endline(string_of_int(LinkedList.data(next_node_2)));

let next_node_3 = LinkedList.next(next_node_2);
print_endline(string_of_int(LinkedList.data(next_node_3)));
/* let next_node_3 = LinkedList.next(next_node_2); */
/* print_endline(string_of_int(LinkedList.data(next_node_3))); */

print_endline("Iterate over the linked list");
LinkedList.iter(item => print_endline(string_of_int(item)), linked_list);

0 comments on commit f835239

Please sign in to comment.