Your function is equivalent to:
let rec elements list = match list with | [] -> [] | h :: t -> elements t
This is because a ; b a ; b evaluates a (and discards the result), and then evaluates and returns b . Obviously, this, in turn, is equivalent to:
let elements (list : 'a list) = []
This is not a very useful feature.
However, before trying to solve this problem, please understand that Objective Caml functions can only return one value . Returning more than one value is not possible.
There are ways around this limitation. One solution is to pack all the values ββthat you want to return into one value: a tuple or a list, as a rule. So, if you need to return an arbitrary number of elements, you must collect them together in a list and process the code of the calling code:
let my_function () = [ 1 ; 2; 3; 4 ] in (* Return four values *) List.iter print_int (my_function ()) (* Print four values *)
Another less frequent solution is to provide a function and call it for each result:
let my_function action = action 1 ; action 2 ; action 3 ; action 4 in my_function print_int
This is less flexible, but perhaps faster than list return: lists can be filtered, sorted, saved ...
source share