-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10.fsx
More file actions
33 lines (25 loc) · 1.07 KB
/
Copy path10.fsx
File metadata and controls
33 lines (25 loc) · 1.07 KB
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
32
33
/// Use the result of problem P09 to implement the so-called run-length
/// encoding data compression method. Consecutive duplicates of elements
/// are encoded as lists (N E) where N is the number of duplicates of the element E.
///
/// Example:
/// * (encode '(a a a a b c c a a d e e e e))
/// ((4 A) (1 B) (2 C) (2 A) (1 D)(4 E))
///
/// Example in F#:
///
/// encode <| List.ofSeq "aaaabccaadeeee"
/// val it : (int * char) list =
/// [(4,'a');(1,'b');(2,'c');(2,'a');(1,'d');(4,'e')]
let combine item = function
| (innerHead::innerTail)::outerTail when innerHead=item -> (item::innerHead::innerTail)::outerTail
| nomatchy -> [item]::nomatchy
let pack list =
List.foldBack combine list []
let runlengthInner item state =
(List.length item, List.head item)::state
let runlength list =
List.foldBack runlengthInner list []
let runLength_after_solution_of_course_use_map l =
l |> List.map (fun x -> List.length x, List.head x)
List.ofSeq "aaaabccaadeeee" |> pack |> runLength_after_solution_of_course_use_map |> printfn "%A"