-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08.fsx
More file actions
31 lines (23 loc) · 947 Bytes
/
Copy path08.fsx
File metadata and controls
31 lines (23 loc) · 947 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//http://www.fssnip.net/an
// Problem 8 : Eliminate consecutive duplicates of list elements.
/// If a list contains repeated elements they should be replaced with a single copy of the
/// element. The order of the elements should not be changed.
///
/// Example:
/// * (compress '(a a a a b c c a a d e e e e))
/// (A B C A D E)
///
/// Example in F#:
///
/// > compress ["a";"a";"a";"a";"b";"c";"c";"a";"a";"d";"e";"e";"e";"e"];;
/// val it : string list = ["a";"b";"c";"a";"d";"e"]
let compress list =
let foldBacker item acc =
//printfn "acc %A item %A" acc item
match acc with
| [] -> [item]
| h::_ when h = item -> acc
| h::_ when h <> item -> item::acc
List.foldBack foldBacker (List.rev list) [] |> List.rev
compress ["a";"a";"a";"a";"b";"c";"c";"a";"a";"d";"e";"e";"e";"e"] |> printfn "%A";;
/// val it : string list = ["a";"b";"c";"a";"d";"e"]