# Developers Planet

Your answer is one click away!

Dengke Liu September 2016

### F#: Remove an element from a self-defined list

Here is a defined list:

`````` type ilist =
E
| 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?

### Answers

Anton Schwaighofer September 2016

You're run into something that is initally really a bit confusing. Very loosely speaking, in pattern matching, you give the thing behind the match statement a new name.

`match (x, l)` means: you take your arguments, break it up in pieces, assign new names. In your second match clause, you essentially say: Ignore the first part of the tuple. For the list, give the head element the name `x`, and the rest the name `l1`. Now this new name `x` shadows your argument `x`. In particular, this is not interpreted as "If the first element of the list is `x`, then do...

#### Post Status

Asked in September 2016
Viewed 1,015 times
Voted 4
Answered 0 times