Re: Page 76 of OCaml by John Whitington ???

"Kenneth Miller [email protected] [ocaml_beginners]" <[email protected]> Mon, 28 Mar 2016 12:12:27 +0000 (UTC)
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <[email protected]>
Right - almost always, if you're using references in a strongly typed functional language, you're going against the idioms and best strengths of the language. It can be hard to think of everything as having a recursive solution, especially since it can often have non-obvious implementation when you first start. Frequently, new functional programmers find themselves repeating the habits they've been taught in imperative languages, and those can be hard to unlearn.

And thank you for your kind words! I'm honored, but I'm just an avid learner with a master's; universities wouldn't accept me. I think I'll pursue a PhD soon, but I have to finish my obligations with my current line of work first. Also, Dr. Sleator's fix of your function clearly illustrates what I've said above while also nudging you away from using references. Thanks Dr. Sleator. 
  

    On Sunday, March 27, 2016 10:21 PM, "[email protected] [ocaml_beginners]" <[email protected]> wrote:
 

     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  #yiv8445544337 #yiv8445544337 -- #yiv8445544337ygrp-mkp {border:1px solid #d8d8d8;font-family:Arial;margin:10px 0;padding:0 10px;}#yiv8445544337 #yiv8445544337ygrp-mkp hr {border:1px solid #d8d8d8;}#yiv8445544337 #yiv8445544337ygrp-mkp #yiv8445544337hd {color:#628c2a;font-size:85%;font-weight:700;line-height:122%;margin:10px 0;}#yiv8445544337 #yiv8445544337ygrp-mkp #yiv8445544337ads {margin-bottom:10px;}#yiv8445544337 #yiv8445544337ygrp-mkp .yiv8445544337ad {padding:0 0;}#yiv8445544337 #yiv8445544337ygrp-mkp .yiv8445544337ad p {margin:0;}#yiv8445544337 #yiv8445544337ygrp-mkp .yiv8445544337ad a {color:#0000ff;text-decoration:none;}#yiv8445544337 #yiv8445544337ygrp-sponsor #yiv8445544337ygrp-lc {font-family:Arial;}#yiv8445544337 #yiv8445544337ygrp-sponsor #yiv8445544337ygrp-lc #yiv8445544337hd {margin:10px 0px;font-weight:700;font-size:78%;line-height:122%;}#yiv8445544337 #yiv8445544337ygrp-sponsor #yiv8445544337ygrp-lc .yiv8445544337ad {margin-bottom:10px;padding:0 0;}#yiv8445544337 #yiv8445544337actions {font-family:Verdana;font-size:11px;padding:10px 0;}#yiv8445544337 #yiv8445544337activity {background-color:#e0ecee;float:left;font-family:Verdana;font-size:10px;padding:10px;}#yiv8445544337 #yiv8445544337activity span {font-weight:700;}#yiv8445544337 #yiv8445544337activity span:first-child {text-transform:uppercase;}#yiv8445544337 #yiv8445544337activity span a {color:#5085b6;text-decoration:none;}#yiv8445544337 #yiv8445544337activity span span {color:#ff7900;}#yiv8445544337 #yiv8445544337activity span .yiv8445544337underline {text-decoration:underline;}#yiv8445544337 .yiv8445544337attach {clear:both;display:table;font-family:Arial;font-size:12px;padding:10px 0;width:400px;}#yiv8445544337 .yiv8445544337attach div a {text-decoration:none;}#yiv8445544337 .yiv8445544337attach img {border:none;padding-right:5px;}#yiv8445544337 .yiv8445544337attach label {display:block;margin-bottom:5px;}#yiv8445544337 .yiv8445544337attach label a {text-decoration:none;}#yiv8445544337 blockquote {margin:0 0 0 4px;}#yiv8445544337 .yiv8445544337bold {font-family:Arial;font-size:13px;font-weight:700;}#yiv8445544337 .yiv8445544337bold a {text-decoration:none;}#yiv8445544337 dd.yiv8445544337last p a {font-family:Verdana;font-weight:700;}#yiv8445544337 dd.yiv8445544337last p span {margin-right:10px;font-family:Verdana;font-weight:700;}#yiv8445544337 dd.yiv8445544337last p span.yiv8445544337yshortcuts {margin-right:0;}#yiv8445544337 div.yiv8445544337attach-table div div a {text-decoration:none;}#yiv8445544337 div.yiv8445544337attach-table {width:400px;}#yiv8445544337 div.yiv8445544337file-title a, #yiv8445544337 div.yiv8445544337file-title a:active, #yiv8445544337 div.yiv8445544337file-title a:hover, #yiv8445544337 div.yiv8445544337file-title a:visited {text-decoration:none;}#yiv8445544337 div.yiv8445544337photo-title a, #yiv8445544337 div.yiv8445544337photo-title a:active, #yiv8445544337 div.yiv8445544337photo-title a:hover, #yiv8445544337 div.yiv8445544337photo-title a:visited {text-decoration:none;}#yiv8445544337 div#yiv8445544337ygrp-mlmsg #yiv8445544337ygrp-msg p a span.yiv8445544337yshortcuts {font-family:Verdana;font-size:10px;font-weight:normal;}#yiv8445544337 .yiv8445544337green {color:#628c2a;}#yiv8445544337 .yiv8445544337MsoNormal {margin:0 0 0 0;}#yiv8445544337 o {font-size:0;}#yiv8445544337 #yiv8445544337photos div {float:left;width:72px;}#yiv8445544337 #yiv8445544337photos div div {border:1px solid #666666;height:62px;overflow:hidden;width:62px;}#yiv8445544337 #yiv8445544337photos div label {color:#666666;font-size:10px;overflow:hidden;text-align:center;white-space:nowrap;width:64px;}#yiv8445544337 #yiv8445544337reco-category {font-size:77%;}#yiv8445544337 #yiv8445544337reco-desc {font-size:77%;}#yiv8445544337 .yiv8445544337replbq {margin:4px;}#yiv8445544337 #yiv8445544337ygrp-actbar div a:first-child {margin-right:2px;padding-right:5px;}#yiv8445544337 #yiv8445544337ygrp-mlmsg {font-size:13px;font-family:Arial, helvetica, clean, sans-serif;}#yiv8445544337 #yiv8445544337ygrp-mlmsg table {font-size:inherit;font:100%;}#yiv8445544337 #yiv8445544337ygrp-mlmsg select, #yiv8445544337 input, #yiv8445544337 textarea {font:99% Arial, Helvetica, clean, sans-serif;}#yiv8445544337 #yiv8445544337ygrp-mlmsg pre, #yiv8445544337 code {font:115% monospace;}#yiv8445544337 #yiv8445544337ygrp-mlmsg * {line-height:1.22em;}#yiv8445544337 #yiv8445544337ygrp-mlmsg #yiv8445544337logo {padding-bottom:10px;}#yiv8445544337 #yiv8445544337ygrp-msg p a {font-family:Verdana;}#yiv8445544337 #yiv8445544337ygrp-msg p#yiv8445544337attach-count span {color:#1E66AE;font-weight:700;}#yiv8445544337 #yiv8445544337ygrp-reco #yiv8445544337reco-head {color:#ff7900;font-weight:700;}#yiv8445544337 #yiv8445544337ygrp-reco {margin-bottom:20px;padding:0px;}#yiv8445544337 #yiv8445544337ygrp-sponsor #yiv8445544337ov li a {font-size:130%;text-decoration:none;}#yiv8445544337 #yiv8445544337ygrp-sponsor #yiv8445544337ov li {font-size:77%;list-style-type:square;padding:6px 0;}#yiv8445544337 #yiv8445544337ygrp-sponsor #yiv8445544337ov ul {margin:0;padding:0 0 0 8px;}#yiv8445544337 #yiv8445544337ygrp-text {font-family:Georgia;}#yiv8445544337 #yiv8445544337ygrp-text p {margin:0 0 1em 0;}#yiv8445544337 #yiv8445544337ygrp-text tt {font-size:120%;}#yiv8445544337 #yiv8445544337ygrp-vital ul li:last-child {border-right:none !important;}#yiv8445544337