Re: pattern matching

"Kenneth Miller [email protected] [ocaml_beginners]" <[email protected]> Tue, 29 Mar 2016 15:11:08 +0000 (UTC)
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <[email protected]>
Patterns are expressions; you can write anything that fits the data type recursively, so long as you obey the type definition. To give you an idea, go back and look at Mr. Herr's definition; a head::second::tail is a list just as much as a [] is or a head::[], get it? Whatever it is that you pattern match, must deconstruct down to some legal construction component parts of a list in order that it can be said that the expression as a whole is a list. 

    On Tuesday, March 29, 2016 11:06 AM, "Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> wrote:
 

     Some of it I got, and some I didn't.  But here's a question.  In pattern matching, can I simply write [head]?  Or am I obligated to write head :: [ ].  They're interchangeable, right?  ( Although the syntax may require one rather than the other.  I would have to experiment with it to find out. )
On Tue, Mar 29, 2016 at 4:02 AM, 'Mr. Herr' [email protected] [ocaml_beginners] <[email protected]> wrote:

      sorry, overlooked sub matching.
 
 You can match several elements in one pattern, just take care to have the "shorter" cases first.
   | []  ->
   | head :: [] -> (* equivalent to [head], all list patterns can be written with square brackets *)
   | head :: second :: rest -> (* here rest can be [] *)
 
 This sounds like you did not understand the explanations I gave.
 
 /Str.
 
 On 29.03.2016 10:18, 'Mr. Herr' [email protected] [ocaml_beginners] 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.
  
 
    

  #yiv0381580366 #yiv0381580366 -- #yiv0381580366ygrp-mkp {border:1px solid #d8d8d8;font-family:Arial;margin:10px 0;padding:0 10px;}#yiv0381580366 #yiv0381580366ygrp-mkp hr {border:1px solid #d8d8d8;}#yiv0381580366 #yiv0381580366ygrp-mkp #yiv0381580366hd {color:#628c2a;font-size:85%;font-weight:700;line-height:122%;margin:10px 0;}#yiv0381580366 #yiv0381580366ygrp-mkp #yiv0381580366ads {margin-bottom:10px;}#yiv0381580366 #yiv0381580366ygrp-mkp .yiv0381580366ad {padding:0 0;}#yiv0381580366 #yiv0381580366ygrp-mkp .yiv0381580366ad p {margin:0;}#yiv0381580366 #yiv0381580366ygrp-mkp .yiv0381580366ad a {color:#0000ff;text-decoration:none;}#yiv0381580366 #yiv0381580366ygrp-sponsor #yiv0381580366ygrp-lc {font-family:Arial;}#yiv0381580366 #yiv0381580366ygrp-sponsor #yiv0381580366ygrp-lc #yiv0381580366hd {margin:10px 0px;font-weight:700;font-size:78%;line-height:122%;}#yiv0381580366 #yiv0381580366ygrp-sponsor #yiv0381580366ygrp-lc .yiv0381580366ad {margin-bottom:10px;padding:0 0;}#yiv0381580366 #yiv0381580366actions {font-family:Verdana;font-size:11px;padding:10px 0;}#yiv0381580366 #yiv0381580366activity {background-color:#e0ecee;float:left;font-family:Verdana;font-size:10px;padding:10px;}#yiv0381580366 #yiv0381580366activity span {font-weight:700;}#yiv0381580366 #yiv0381580366activity span:first-child {text-transform:uppercase;}#yiv0381580366 #yiv0381580366activity span a {color:#5085b6;text-decoration:none;}#yiv0381580366 #yiv0381580366activity span span {color:#ff7900;}#yiv0381580366 #yiv0381580366activity span .yiv0381580366underline {text-decoration:underline;}#yiv0381580366 .yiv0381580366attach {clear:both;display:table;font-family:Arial;font-size:12px;padding:10px 0;width:400px;}#yiv0381580366 .yiv0381580366attach div a {text-decoration:none;}#yiv0381580366 .yiv0381580366attach img {border:none;padding-right:5px;}#yiv0381580366 .yiv0381580366attach label {display:block;margin-bottom:5px;}#yiv0381580366 .yiv0381580366attach label a {text-decoration:none;}#yiv0381580366 blockquote {margin:0 0 0 4px;}#yiv0381580366 .yiv0381580366bold {font-family:Arial;font-size:13px;font-weight:700;}#yiv0381580366 .yiv0381580366bold a {text-decoration:none;}#yiv0381580366 dd.yiv0381580366last p a {font-family:Verdana;font-weight:700;}#yiv0381580366 dd.yiv0381580366last p span {margin-right:10px;font-family:Verdana;font-weight:700;}#yiv0381580366 dd.yiv0381580366last p span.yiv0381580366yshortcuts {margin-right:0;}#yiv0381580366 div.yiv0381580366attach-table div div a {text-decoration:none;}#yiv0381580366 div.yiv0381580366attach-table {width:400px;}#yiv0381580366 div.yiv0381580366file-title a, #yiv0381580366 div.yiv0381580366file-title a:active, #yiv0381580366 div.yiv0381580366file-title a:hover, #yiv0381580366 div.yiv0381580366file-title a:visited {text-decoration:none;}#yiv0381580366 div.yiv0381580366photo-title a, #yiv0381580366 div.yiv0381580366photo-title a:active, #yiv0381580366 div.yiv0381580366photo-title a:hover, #yiv0381580366 div.yiv0381580366photo-title a:visited {text-decoration:none;}#yiv0381580366 div#yiv0381580366ygrp-mlmsg #yiv0381580366ygrp-msg p a span.yiv0381580366yshortcuts {font-family:Verdana;font-size:10px;font-weight:normal;}#yiv0381580366 .yiv0381580366green {color:#628c2a;}#yiv0381580366 .yiv0381580366MsoNormal {margin:0 0 0 0;}#yiv0381580366 o {font-size:0;}#yiv0381580366 #yiv0381580366photos div {float:left;width:72px;}#yiv0381580366 #yiv0381580366photos div div {border:1px solid #666666;height:62px;overflow:hidden;width:62px;}#yiv0381580366 #yiv0381580366photos div label {color:#666666;font-size:10px;overflow:hidden;text-align:center;white-space:nowrap;width:64px;}#yiv0381580366 #yiv0381580366reco-category {font-size:77%;}#yiv0381580366 #yiv0381580366reco-desc {font-size:77%;}#yiv0381580366 .yiv0381580366replbq {margin:4px;}#yiv0381580366 #yiv0381580366ygrp-actbar div a:first-child {margin-right:2px;padding-right:5px;}#yiv0381580366 #yiv0381580366ygrp-mlmsg {font-size:13px;font-family:Arial, helvetica, clean, sans-serif;}#yiv0381580366 #yiv0381580366ygrp-mlmsg table {font-size:inherit;font:100%;}#yiv0381580366 #yiv0381580366ygrp-mlmsg select, #yiv0381580366 input, #yiv0381580366 textarea {font:99% Arial, Helvetica, clean, sans-serif;}#yiv0381580366 #yiv0381580366ygrp-mlmsg pre, #yiv0381580366 code {font:115% monospace;}#yiv0381580366 #yiv0381580366ygrp-mlmsg * {line-height:1.22em;}#yiv0381580366 #yiv0381580366ygrp-mlmsg #yiv0381580366logo {padding-bottom:10px;}#yiv0381580366 #yiv0381580366ygrp-msg p a {font-family:Verdana;}#yiv0381580366 #yiv0381580366ygrp-msg p#yiv0381580366attach-count span {color:#1E66AE;font-weight:700;}#yiv0381580366 #yiv0381580366ygrp-reco #yiv0381580366reco-head {color:#ff7900;font-weight:700;}#yiv0381580366 #yiv0381580366ygrp-reco {margin-bottom:20px;padding:0px;}#yiv0381580366 #yiv0381580366ygrp-sponsor #yiv0381580366ov li a {font-size:130%;text-decoration:none;}#yiv0381580366 #yiv0381580366ygrp-sponsor #yiv0381580366ov li {font-size:77%;list-style-type:square;padding:6px 0;}#yiv0381580366 #yiv0381580366ygrp-sponsor #yiv0381580366ov ul {margin:0;padding:0 0 0 8px;}#yiv0381580366 #yiv0381580366ygrp-text {font-family:Georgia;}#yiv0381580366 #yiv0381580366ygrp-text p {margin:0 0 1em 0;}#yiv0381580366 #yiv0381580366ygrp-text tt {font-size:120%;}#yiv0381580366 #yiv0381580366ygrp-vital ul li:last-child {border-right:none !important;}#yiv0381580366