I finally have my own solution.
"Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> Wed, 30 Mar 2016 23:25:57 -0500
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <CAM0XMJRg78xEmByY_EcHtdHV7D84wp3qaSjM-OXmeDj4R7P6=A@mail.gmail.com> |
So am I ready to become the director of the Ocaml Project at INRIA in France? :-) Well maybe not, but I'm proud of this. It took some practice to finally figure this out. Definitely a challenging language! But the code is pretty compact, which is an appealing characteristic of the language. And even if I never use Ocaml professionally, it's fun because it's really helping me to understand how recursion works. I also like the idea of embedding recursive functions inside non-recursive functions. Pretty interesting stuff for sure! *let pack list = * *let rec pack_ l sub out = * *match l with * *|[ ] -> !out* *|head :: tail -> if tail = [ ] then begin sub := [head] @ !sub; out:= !sub :: !out; List.rev !out end * *else if head = ( List.hd tail ) then begin sub := [head] @ !sub ; pack_ ( List.tl l ) sub out end * *else begin sub := [head] @ !sub ; out := !sub :: !out ; pack_ ( List.tl l ) ( ref [ ] ) out end * *in * *pack_ list ( ref [ ] ) ( ref [ ] ) ;; * Best, Douglas.