Re: pattern matching
"Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> Tue, 29 Mar 2016 10:06:50 -0500
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <CAM0XMJRhVHsxs6JLJgxEsRqs2XtuQj5Uur=c3DjvJdVec8M=jg@mail.gmail.com> |
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. > > > >