Re: Page 76 of OCaml by John Whitington ???
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <[email protected]> |
Part of the problem is that the "reference solution" is incomprehensible. Here's how I did it:
let pack li =
let rec grab_block li ac =
(* Grab a block of equal elements from li and put them into ac.
Return the pair (li,ac). *)
match li with
| [] -> ([],ac)
| x::tail ->
if ac=[] || x = List.hd ac then grab_block tail (x::ac)
else (li, ac)
in
let rec loop li ac = if li = [] then List.rev ac else
let (rest,bl) = grab_block li [] in
loop rest (bl::ac)
in
loop li []
I think this is much easier to understand, and just as efficient.
---DS