Re: Page 76 of OCaml by John Whitington ???
"Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> Tue, 29 Mar 2016 10:37:51 -0500
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <CAM0XMJSsRNMiEbU=4-QjR_ry9kJh+j2d4r1i9QEdN=-4YBk5+A@mail.gmail.com> |
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* > *|[ ] -> false* > *head :: 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. > > > > > >