title: F# Removing duplicates from list with function
body: <p>I want to create a function that takes a list and returns a list with removed duplicates.</p>
  
  <pre><code>let removedupes list1 =
    let list2 = []
    let rec removeduprec list1 list2 =
      match list1 with
      | [] -&gt; list2
      | head :: tail when mem list2 head = false -&gt; head :: removeduprec tail list2
      | _ -&gt; removeduprec list1.Tail list2
    removeduprec list1 list2
  </code></pre>
  
  <p>Im using this "mem" function to go trough the list and see if the value already exists and in that case i want to continue with the recursion.</p>
  
  <pre><code>let rec mem list x = 
    match list with
    | [] -&gt; false
    | head :: tail -&gt; 
      if x = head then true else mem tail x 
  </code></pre>
  
  <p>When i test this code i get</p>
  
  <pre><code>let list1 =  [ 1; 2; 3; 4; 5; 2; 2; 2]
  removedups list1;;
  val it : int list = [1; 2; 3; 4; 5; 2; 2; 2]
  </code></pre>
  
  <p>Im thinking that the "head :: removeduprec tail list2", but im quite new to f# so not completely sure how this works.</p>