parent
e869be4852
commit
6a59fdf97f
41
README.org
41
README.org
@ -198,7 +198,6 @@ let rec pack list =
|
|||||||
then f (x::acc1) acc2 tail
|
then f (x::acc1) acc2 tail
|
||||||
else f [] ((x::acc1)::acc2) tail
|
else f [] ((x::acc1)::acc2) tail
|
||||||
in f [] [] list |> List.rev
|
in f [] [] list |> List.rev
|
||||||
|
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
@ -250,18 +249,23 @@ type 'a rle =
|
|||||||
|
|
||||||
**** Solution
|
**** Solution
|
||||||
#+begin_src ocaml
|
#+begin_src ocaml
|
||||||
let encode = function
|
let rec pack list =
|
||||||
| [] -> []
|
let rec f acc1 acc2 = function
|
||||||
| list ->
|
| [] -> []
|
||||||
let rec f (count, elem) acc2 = function
|
| [x] -> (x::acc1)::acc2
|
||||||
| [] -> []
|
| x::(y::xs as tail) ->
|
||||||
| [x] when count = 0 -> (One x)::acc2
|
if x = y
|
||||||
| [x] -> (Many (count + 1, x))::acc2
|
then f (x::acc1) acc2 tail
|
||||||
| x::(y::xs as tail) when x = y -> f (count + 1, elem) acc2 tail
|
else f [] ((x::acc1)::acc2) tail
|
||||||
| x::(y::xs as tail) when count = 0 -> f (0, y) ((One x)::acc2) tail
|
in f [] [] list |> List.rev
|
||||||
| x::(y::xs as tail) -> f (0, y) ((Many (count + 1, x))::acc2) tail
|
|
||||||
in f (0, List.nth list 0) [] list |> List.rev
|
|
||||||
|
|
||||||
|
let encode list =
|
||||||
|
let packed = pack list
|
||||||
|
in packed |>
|
||||||
|
List.map (function
|
||||||
|
| [] -> invalid_arg "List should not be empty"
|
||||||
|
| [x] -> One x
|
||||||
|
| x::xs as l -> Many (List.length l, x))
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
@ -306,7 +310,17 @@ result list by replacing the singleton lists (1 X) by X.
|
|||||||
|
|
||||||
**** Solution
|
**** Solution
|
||||||
#+begin_src ocaml
|
#+begin_src ocaml
|
||||||
|
let encode = function
|
||||||
|
| [] -> []
|
||||||
|
| list ->
|
||||||
|
let rec f (count, elem) acc2 = function
|
||||||
|
| [] -> []
|
||||||
|
| [x] when count = 0 -> (One x)::acc2
|
||||||
|
| [x] -> (Many (count + 1, x))::acc2
|
||||||
|
| x::(y::xs as tail) when x = y -> f (count + 1, elem) acc2 tail
|
||||||
|
| x::(y::xs as tail) when count = 0 -> f (0, y) ((One x)::acc2) tail
|
||||||
|
| x::(y::xs as tail) -> f (0, y) ((Many (count + 1, x))::acc2) tail
|
||||||
|
in f (0, List.nth list 0) [] list |> List.rev
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
@ -368,4 +382,3 @@ let drop list num =
|
|||||||
in f (num,[]) list |> List.rev
|
in f (num,[]) list |> List.rev
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user