Hello. As in your other post, I will try to give you an idea without giving the full solution.
This is a recursive function on a list. The two constructors for a list are []
and ::
, meaning that every list is either []
or x :: xs
with x
an element and xs
another list.
As a first idea, you can match the input list and see what you need to do in each case:
let rec remove_half1 l =
match l with
| [] -> (* what to do in the empty case? *)
| x :: xs -> (* what to do with x? what to do with xs? *)
The problem is that here you want to do a different thing with x
according to the position in the list (even or odd). It means you need to know the position when you match on l
, so your function must have an additional argument to remember the position.
let rec remove_half2 n l =
match l with
| [] -> (* ... *)
| x :: xs -> if (* test on n *) then (* ... *) else (* ... *)
If you manage to write this remove_half2
it will have the following type:
remove_half2 : int -> `a list -> `a list
but remove_half
does not have this int
parameter. It means that you need to run remove_half2
with an initial value so that you have the right type for your final function.
let remove_half l = remove_half2 (* initial value *) l
Good luck!