Re: Page 76 of OCaml by John Whitington ???
"Kenneth Miller [email protected] [ocaml_beginners]" <[email protected]> Tue, 29 Mar 2016 15:24:05 +0000 (UTC)
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <[email protected]> |
Ok, I agree, but you can simplify your pattern matching of the second part... I'm guessing what you meant to write was:
let rec practice_function list = match list with|[ ] -> false| head :: tail -> match tail with|[ ] -> false|headOfTail :: tailOfTail -> if head = headOfTail then true else practice_function tail ;;
You could just write | [] | head::[] -> false | headOf...Chaining multiple patterns together tells the compiler to map the same action to the same clause.
I can't really understand your intentions with the headOfTail :: tailOfTail part, but it seems you are testing head= headOfTail, which can also be simplified if the overall behavior of the function is to be retained with this, supposing you use what I gave above also.
| head::second::remaining -> if head = second then ...
On Tuesday, March 29, 2016 11:17 AM, "Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> wrote:
Yes, tailOfTail is a list, but headOfTail is an element ( which could be a list if we're dealing with a list of lists ).
So if I have something like:
let rec practice_function list = match list with|[ ] -> falsehead :: tail -> match tail with|[ ] -> false|headOfTail :: tailOfTail -> if head = headOfTail then true else practice_function tail ;;
I should think that the above function would test the list for equal elements that are right next to each other in the list. I was trying to base my packing function on similar logic, but my approach became horribly convoluted and it just didn't work. I think my logic is sound, but my implementation was not effective.
On Tue, Mar 29, 2016 at 3:18 AM, 'Mr. Herr' [email protected] [ocaml_beginners] <[email protected]> wrote:
On 29.03.2016 06:41, Douglas Lewit [email protected] [ocaml_beginners] wrote:
I also gave something like this a try, but I ended up with a couple of error messages:
let pack list = match list with |[ ] -> [ ] |head :: tail -> match tail with |[ ] -> [ ] headOfTail :: tailOfTail -> if head = headOfTail then..... ( more code )
I thought it was clever, but either I just ended up with an empty list, a one-element list, or the compiler just rejected it. I'm starting to feel a little dumb! But I'll keep at it.
tailOfTail is a list, not an element!
/Str.
#yiv3375993906 #yiv3375993906 -- #yiv3375993906ygrp-mkp {border:1px solid #d8d8d8;font-family:Arial;margin:10px 0;padding:0 10px;}#yiv3375993906 #yiv3375993906ygrp-mkp hr {border:1px solid #d8d8d8;}#yiv3375993906 #yiv3375993906ygrp-mkp #yiv3375993906hd {color:#628c2a;font-size:85%;font-weight:700;line-height:122%;margin:10px 0;}#yiv3375993906 #yiv3375993906ygrp-mkp #yiv3375993906ads {margin-bottom:10px;}#yiv3375993906 #yiv3375993906ygrp-mkp .yiv3375993906ad {padding:0 0;}#yiv3375993906 #yiv3375993906ygrp-mkp .yiv3375993906ad p {margin:0;}#yiv3375993906 #yiv3375993906ygrp-mkp .yiv3375993906ad a {color:#0000ff;text-decoration:none;}#yiv3375993906 #yiv3375993906ygrp-sponsor #yiv3375993906ygrp-lc {font-family:Arial;}#yiv3375993906 #yiv3375993906ygrp-sponsor #yiv3375993906ygrp-lc #yiv3375993906hd {margin:10px 0px;font-weight:700;font-size:78%;line-height:122%;}#yiv3375993906 #yiv3375993906ygrp-sponsor #yiv3375993906ygrp-lc .yiv3375993906ad {margin-bottom:10px;padding:0 0;}#yiv3375993906 #yiv3375993906actions {font-family:Verdana;font-size:11px;padding:10px 0;}#yiv3375993906 #yiv3375993906activity {background-color:#e0ecee;float:left;font-family:Verdana;font-size:10px;padding:10px;}#yiv3375993906 #yiv3375993906activity span {font-weight:700;}#yiv3375993906 #yiv3375993906activity span:first-child {text-transform:uppercase;}#yiv3375993906 #yiv3375993906activity span a {color:#5085b6;text-decoration:none;}#yiv3375993906 #yiv3375993906activity span span {color:#ff7900;}#yiv3375993906 #yiv3375993906activity span .yiv3375993906underline {text-decoration:underline;}#yiv3375993906 .yiv3375993906attach {clear:both;display:table;font-family:Arial;font-size:12px;padding:10px 0;width:400px;}#yiv3375993906 .yiv3375993906attach div a {text-decoration:none;}#yiv3375993906 .yiv3375993906attach img {border:none;padding-right:5px;}#yiv3375993906 .yiv3375993906attach label {display:block;margin-bottom:5px;}#yiv3375993906 .yiv3375993906attach label a {text-decoration:none;}#yiv3375993906 blockquote {margin:0 0 0 4px;}#yiv3375993906 .yiv3375993906bold {font-family:Arial;font-size:13px;font-weight:700;}#yiv3375993906 .yiv3375993906bold a {text-decoration:none;}#yiv3375993906 dd.yiv3375993906last p a {font-family:Verdana;font-weight:700;}#yiv3375993906 dd.yiv3375993906last p span {margin-right:10px;font-family:Verdana;font-weight:700;}#yiv3375993906 dd.yiv3375993906last p span.yiv3375993906yshortcuts {margin-right:0;}#yiv3375993906 div.yiv3375993906attach-table div div a {text-decoration:none;}#yiv3375993906 div.yiv3375993906attach-table {width:400px;}#yiv3375993906 div.yiv3375993906file-title a, #yiv3375993906 div.yiv3375993906file-title a:active, #yiv3375993906 div.yiv3375993906file-title a:hover, #yiv3375993906 div.yiv3375993906file-title a:visited {text-decoration:none;}#yiv3375993906 div.yiv3375993906photo-title a, #yiv3375993906 div.yiv3375993906photo-title a:active, #yiv3375993906 div.yiv3375993906photo-title a:hover, #yiv3375993906 div.yiv3375993906photo-title a:visited {text-decoration:none;}#yiv3375993906 div#yiv3375993906ygrp-mlmsg #yiv3375993906ygrp-msg p a span.yiv3375993906yshortcuts {font-family:Verdana;font-size:10px;font-weight:normal;}#yiv3375993906 .yiv3375993906green {color:#628c2a;}#yiv3375993906 .yiv3375993906MsoNormal {margin:0 0 0 0;}#yiv3375993906 o {font-size:0;}#yiv3375993906 #yiv3375993906photos div {float:left;width:72px;}#yiv3375993906 #yiv3375993906photos div div {border:1px solid #666666;height:62px;overflow:hidden;width:62px;}#yiv3375993906 #yiv3375993906photos div label {color:#666666;font-size:10px;overflow:hidden;text-align:center;white-space:nowrap;width:64px;}#yiv3375993906 #yiv3375993906reco-category {font-size:77%;}#yiv3375993906 #yiv3375993906reco-desc {font-size:77%;}#yiv3375993906 .yiv3375993906replbq {margin:4px;}#yiv3375993906 #yiv3375993906ygrp-actbar div a:first-child {margin-right:2px;padding-right:5px;}#yiv3375993906 #yiv3375993906ygrp-mlmsg {font-size:13px;font-family:Arial, helvetica, clean, sans-serif;}#yiv3375993906 #yiv3375993906ygrp-mlmsg table {font-size:inherit;font:100%;}#yiv3375993906 #yiv3375993906ygrp-mlmsg select, #yiv3375993906 input, #yiv3375993906 textarea {font:99% Arial, Helvetica, clean, sans-serif;}#yiv3375993906 #yiv3375993906ygrp-mlmsg pre, #yiv3375993906 code {font:115% monospace;}#yiv3375993906 #yiv3375993906ygrp-mlmsg * {line-height:1.22em;}#yiv3375993906 #yiv3375993906ygrp-mlmsg #yiv3375993906logo {padding-bottom:10px;}#yiv3375993906 #yiv3375993906ygrp-msg p a {font-family:Verdana;}#yiv3375993906 #yiv3375993906ygrp-msg p#yiv3375993906attach-count span {color:#1E66AE;font-weight:700;}#yiv3375993906 #yiv3375993906ygrp-reco #yiv3375993906reco-head {color:#ff7900;font-weight:700;}#yiv3375993906 #yiv3375993906ygrp-reco {margin-bottom:20px;padding:0px;}#yiv3375993906 #yiv3375993906ygrp-sponsor #yiv3375993906ov li a {font-size:130%;text-decoration:none;}#yiv3375993906 #yiv3375993906ygrp-sponsor #yiv3375993906ov li {font-size:77%;list-style-type:square;padding:6px 0;}#yiv3375993906 #yiv3375993906ygrp-sponsor #yiv3375993906ov ul {margin:0;padding:0 0 0 8px;}#yiv3375993906 #yiv3375993906ygrp-text {font-family:Georgia;}#yiv3375993906 #yiv3375993906ygrp-text p {margin:0 0 1em 0;}#yiv3375993906 #yiv3375993906ygrp-text tt {font-size:120%;}#yiv3375993906 #yiv3375993906ygrp-vital ul li:last-child {border-right:none !important;}#yiv3375993906