Reputation: 553
Seems simple enough, but I'm very new to this language and having some trouble. Given two lists, what would be the best way to write a function to determine which list is "larger."
For example: [1:2:3] and [1:3:2] would return [1:3:2]
These lists don't have to be the same length: [1:2] and [1:2:3] would return [1:2:3]
Thanks.
Upvotes: 2
Views: 2990
Reputation: 66823
The predefined function max
will do this for you:
# max [1;2;3] [1;3;2];;
- : int list = [1; 3; 2]
# max [1;2] [1;2;3];;
- : int list = [1; 2; 3]
Of course, it depends on what you mean by "larger". The built-in comparisons of OCaml use lexicographic order. If you wanted to use some other order, you would actually have to write your own function.
Or maybe you want to write your own function from scratch just for the practice. In that case, a good way to go with lists in OCaml is to use recursion. Try out some patterns of recursion and (if you still need help), update your question to show what you've tried.
Upvotes: 4