Re: Page 76 of OCaml by John Whitington ???
"Kenneth Miller [email protected] [ocaml_beginners]" <[email protected]> Tue, 29 Mar 2016 15:50:06 +0000 (UTC)
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <[email protected]> |
Once you read it a ton of times, you see it and you immediately know what it means. Also, if you know the type definition, you know the component parts of a deconstructive pattern match. The functional programming community is very close to mathematics; their conventions aren't going to change, and you really only ever have control over your own code. If I could wish away the poor foundations of most of the rest of the world's code, I would, but in exchange a higher learning curve isn't too bad of a price to pay.
Also, since you probably don't want to while away your hours constantly looking up definitions, I highly recommend that you use merlin and ocamlspotter with the Emacs. While I don't want to convert you to an editor or say that you have to use them, they do work together very nicely. In fact, I will say a few words: interactive type introspection, intellisense style auto completion and fully build system integrated.
On Tuesday, March 29, 2016 11:37 AM, "Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> wrote:
Hi Ken,
Yes, I understand head :: tail :: remaining..... that makes sense to me. What bothers me about some Ocaml code is the use of too many abbreviations and very short variable names. For example, on the web I have noticed a lot of Ocaml code where you have something like
|[ ] -> some code| h :: t -> some more code
I know what h :: t is, but I think the program is more readable when people use more meaningful variable names, such as head :: tail -> or headOfList :: tailOfList, etc, etc. It just makes the program more readable so that another programmer can look at it and say, "Oh yes, I think I understand what this person was trying to do". But these days everybody wants to abbreviate everything! I remember my confusion when someone sent me an email with the abbreviation, LOL. I thought, "What on earth is LOL?" Someone had to tell me, "Doug, LOL is short for laughing out loud!" But I think it's important to choose meaningful variable names rather than just x, y, z, h, t, etc. The compiler won't care of course, but my brain is not an Ocaml compiler! That's why I get a little annoyed when programmers use just one-letter variable names. Personally, I like to spell things out in greater detail for the sake of clarity. I think it really makes a difference.
On Tue, Mar 29, 2016 at 10:24 AM, Kenneth Miller [email protected] [ocaml_beginners] <[email protected]> wrote:
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.
#yiv6710081335 #yiv6710081335 -- #yiv6710081335ygrp-mkp {border:1px solid #d8d8d8;font-family:Arial;margin:10px 0;padding:0 10px;}#yiv6710081335 #yiv6710081335ygrp-mkp hr {border:1px solid #d8d8d8;}#yiv6710081335 #yiv6710081335ygrp-mkp #yiv6710081335hd {color:#628c2a;font-size:85%;font-weight:700;line-height:122%;margin:10px 0;}#yiv6710081335 #yiv6710081335ygrp-mkp #yiv6710081335ads {margin-bottom:10px;}#yiv6710081335 #yiv6710081335ygrp-mkp .yiv6710081335ad {padding:0 0;}#yiv6710081335 #yiv6710081335ygrp-mkp .yiv6710081335ad p {margin:0;}#yiv6710081335 #yiv6710081335ygrp-mkp .yiv6710081335ad a {color:#0000ff;text-decoration:none;}#yiv6710081335 #yiv6710081335ygrp-sponsor #yiv6710081335ygrp-lc {font-family:Arial;}#yiv6710081335 #yiv6710081335ygrp-sponsor #yiv6710081335ygrp-lc #yiv6710081335hd {margin:10px 0px;font-weight:700;font-size:78%;line-height:122%;}#yiv6710081335 #yiv6710081335ygrp-sponsor #yiv6710081335ygrp-lc .yiv6710081335ad {margin-bottom:10px;padding:0 0;}#yiv6710081335 #yiv6710081335actions {font-family:Verdana;font-size:11px;padding:10px 0;}#yiv6710081335 #yiv6710081335activity {background-color:#e0ecee;float:left;font-family:Verdana;font-size:10px;padding:10px;}#yiv6710081335 #yiv6710081335activity span {font-weight:700;}#yiv6710081335 #yiv6710081335activity span:first-child {text-transform:uppercase;}#yiv6710081335 #yiv6710081335activity span a {color:#5085b6;text-decoration:none;}#yiv6710081335 #yiv6710081335activity span span {color:#ff7900;}#yiv6710081335 #yiv6710081335activity span .yiv6710081335underline {text-decoration:underline;}#yiv6710081335 .yiv6710081335attach {clear:both;display:table;font-family:Arial;font-size:12px;padding:10px 0;width:400px;}#yiv6710081335 .yiv6710081335attach div a {text-decoration:none;}#yiv6710081335 .yiv6710081335attach img {border:none;padding-right:5px;}#yiv6710081335 .yiv6710081335attach label {display:block;margin-bottom:5px;}#yiv6710081335 .yiv6710081335attach label a {text-decoration:none;}#yiv6710081335 blockquote {margin:0 0 0 4px;}#yiv6710081335 .yiv6710081335bold {font-family:Arial;font-size:13px;font-weight:700;}#yiv6710081335 .yiv6710081335bold a {text-decoration:none;}#yiv6710081335 dd.yiv6710081335last p a {font-family:Verdana;font-weight:700;}#yiv6710081335 dd.yiv6710081335last p span {margin-right:10px;font-family:Verdana;font-weight:700;}#yiv6710081335 dd.yiv6710081335last p span.yiv6710081335yshortcuts {margin-right:0;}#yiv6710081335 div.yiv6710081335attach-table div div a {text-decoration:none;}#yiv6710081335 div.yiv6710081335attach-table {width:400px;}#yiv6710081335 div.yiv6710081335file-title a, #yiv6710081335 div.yiv6710081335file-title a:active, #yiv6710081335 div.yiv6710081335file-title a:hover, #yiv6710081335 div.yiv6710081335file-title a:visited {text-decoration:none;}#yiv6710081335 div.yiv6710081335photo-title a, #yiv6710081335 div.yiv6710081335photo-title a:active, #yiv6710081335 div.yiv6710081335photo-title a:hover, #yiv6710081335 div.yiv6710081335photo-title a:visited {text-decoration:none;}#yiv6710081335 div#yiv6710081335ygrp-mlmsg #yiv6710081335ygrp-msg p a span.yiv6710081335yshortcuts {font-family:Verdana;font-size:10px;font-weight:normal;}#yiv6710081335 .yiv6710081335green {color:#628c2a;}#yiv6710081335 .yiv6710081335MsoNormal {margin:0 0 0 0;}#yiv6710081335 o {font-size:0;}#yiv6710081335 #yiv6710081335photos div {float:left;width:72px;}#yiv6710081335 #yiv6710081335photos div div {border:1px solid #666666;height:62px;overflow:hidden;width:62px;}#yiv6710081335 #yiv6710081335photos div label {color:#666666;font-size:10px;overflow:hidden;text-align:center;white-space:nowrap;width:64px;}#yiv6710081335 #yiv6710081335reco-category {font-size:77%;}#yiv6710081335 #yiv6710081335reco-desc {font-size:77%;}#yiv6710081335 .yiv6710081335replbq {margin:4px;}#yiv6710081335 #yiv6710081335ygrp-actbar div a:first-child {margin-right:2px;padding-right:5px;}#yiv6710081335 #yiv6710081335ygrp-mlmsg {font-size:13px;font-family:Arial, helvetica, clean, sans-serif;}#yiv6710081335 #yiv6710081335ygrp-mlmsg table {font-size:inherit;font:100%;}#yiv6710081335 #yiv6710081335ygrp-mlmsg select, #yiv6710081335 input, #yiv6710081335 textarea {font:99% Arial, Helvetica, clean, sans-serif;}#yiv6710081335 #yiv6710081335ygrp-mlmsg pre, #yiv6710081335 code {font:115% monospace;}#yiv6710081335 #yiv6710081335ygrp-mlmsg * {line-height:1.22em;}#yiv6710081335 #yiv6710081335ygrp-mlmsg #yiv6710081335logo {padding-bottom:10px;}#yiv6710081335 #yiv6710081335ygrp-msg p a {font-family:Verdana;}#yiv6710081335 #yiv6710081335ygrp-msg p#yiv6710081335attach-count span {color:#1E66AE;font-weight:700;}#yiv6710081335 #yiv6710081335ygrp-reco #yiv6710081335reco-head {color:#ff7900;font-weight:700;}#yiv6710081335 #yiv6710081335ygrp-reco {margin-bottom:20px;padding:0px;}#yiv6710081335 #yiv6710081335ygrp-sponsor #yiv6710081335ov li a {font-size:130%;text-decoration:none;}#yiv6710081335 #yiv6710081335ygrp-sponsor #yiv6710081335ov li {font-size:77%;list-style-type:square;padding:6px 0;}#yiv6710081335 #yiv6710081335ygrp-sponsor #yiv6710081335ov ul {margin:0;padding:0 0 0 8px;}#yiv6710081335 #yiv6710081335ygrp-text {font-family:Georgia;}#yiv6710081335 #yiv6710081335ygrp-text p {margin:0 0 1em 0;}#yiv6710081335 #yiv6710081335ygrp-text tt {font-size:120%;}#yiv6710081335 #yiv6710081335ygrp-vital ul li:last-child {border-right:none !important;}#yiv6710081335