Here is a defined list:
type ilist =
| L of int * ilist
where the constructor E stands for the empty list and the constructor L builds a list by adding a number in front of another list.
Then one can represent, say, a list with elements 1,4,6,7, in that order, with the ilist value: L(1, L(4, L(6, L(7, E))))
Now I need to implement a remove() funciton to remove all occurences of a element from the list. For example, remove 2 (L(1, L(2, L(3, L(3, L(2, E)))))) is L(1, L(3, L(3, E))).
Here is my solution:
let rec remove (x:int) (l:list)=
match (x, l) with
| (_, E)->E
| (_, L(x, l1)) -> remove (x) (l1)
| (_, L(y, l1)) -> L(y, remove (x)(l1)) // Warning: This line will never be matched
As you see, the third case will never be matched. How can I deal with the case such that I can maintain the element in the list if it is not x?