4 people like it.
Like the snippet!
Remove duplicate list elements
I have been working on my "Functional Style" and like to think that I have improved a bit.
The list order is no longer maintained in this snippet; however as the order change is simply a feature of the foldBack process the reversal of input and reversal of output is a reliable (and efficient?) fix.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
|
let removeDuplicates(m_list : 'a list) : 'a list =
let reversedList = m_list |> List.rev // filter elements in original order
let rec filterOutDuplicate i acc =
if i < 0 then
acc |> List.rev // result returned in original order
else
if List.exists(fun y -> reversedList.[i] = y) acc then
filterOutDuplicate (i - 1) (acc)
else // function starts at last element of list
filterOutDuplicate (i - 1) (reversedList.[i] :: acc)
filterOutDuplicate (m_list.Length - 1) List.empty
let testlist = ["e"; "j"; "f"; "h"; "d"; "i"; "k"; "l"; "g"; "a"; "b"; "c"; "d"; "e"; "f"; "g" ]
let removeDupL9 = removeDuplicates(testlist)
|
val removeDuplicates : m_list:'a list -> 'a list (requires equality)
Full name: Script.removeDuplicates
val m_list : 'a list (requires equality)
type 'T list = List<'T>
Full name: Microsoft.FSharp.Collections.list<_>
val reversedList : 'a list (requires equality)
Multiple items
module List
from Microsoft.FSharp.Collections
--------------------
type List<'T> =
| ( [] )
| ( :: ) of Head: 'T * Tail: 'T list
interface IEnumerable
interface IEnumerable<'T>
member Head : 'T
member IsEmpty : bool
member Item : index:int -> 'T with get
member Length : int
member Tail : 'T list
static member Cons : head:'T * tail:'T list -> 'T list
static member Empty : 'T list
Full name: Microsoft.FSharp.Collections.List<_>
val rev : list:'T list -> 'T list
Full name: Microsoft.FSharp.Collections.List.rev
val filterOutDuplicate : (int -> 'a list -> 'a list) (requires equality)
val i : int
val acc : 'a list (requires equality)
val exists : predicate:('T -> bool) -> list:'T list -> bool
Full name: Microsoft.FSharp.Collections.List.exists
val y : 'a (requires equality)
property List.Length: int
val empty<'T> : 'T list
Full name: Microsoft.FSharp.Collections.List.empty
val testlist : string list
Full name: Script.testlist
val removeDupL9 : string list
Full name: Script.removeDupL9
More information