Re: pattern matching
"Kenneth Miller [email protected] [ocaml_beginners]" <[email protected]> Tue, 29 Mar 2016 16:00:43 +0000 (UTC)
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <[email protected]> |
I didn't mean to confuse you Doug, I'll explain each in turn.
You can call C functions with ocaml-ctypes, which is actually a wrapper library to some inherent inbuilt functionality that derives all the way from some language primitives ("external" keyword). It just makes it easier for you.
YNot is a paper about a Coq module that facilitates reasoning with Hoare Type Theory. I thought was really excellent by Harvard's Greg Morrisett. It strives to capture co-program-proof construction spirit in Coq, which so far has a superb kernel and fantastic CIC foundation, but that has been lacking monad style hoare reasoning for a while. He integrated the reasoning basis into the type system so that you can write things like ring buffers and hash tables and reason about their correctness while still allowing side effects.
Basically, no one wants to pay the cost of re-implementing software, and for good reason. Software is difficult to maintain and very expensive. So people take shortcuts in all languages.
In my opinion, the foundational definitions of types and their default behavior are critical because you want to express to the user in each class of situations what has happened. In the case of having behavior default to nil or to some other type instance, you haven't actually expressed back to the user what should be done; the designers just assume 0 or nil will be good for you. You then end up doing the extra work to learn the individual behaviors of various functions in order that you not step on a land mine. That's much more expensive than getting a type error where the compiler tells you the cases that you haven't considered. (And which you can introspect with merlin!)
On Tuesday, March 29, 2016 11:42 AM, "Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> wrote:
Hi Ken,
What do you mean that "every language has an escape hatch to C"? I'm not familiar with YNot. Judging by the context, I would assume that YNot is some Ocaml module that allows Ocaml to directly implement C code. I know they do this occasionally in Python and Ruby for the sake of speed, but why is this necessary in a language like Ocaml? I thought Ocaml was just as fast as anything written in C?
Ruby is a nice language, but at times it appears to be a little too forgiving. ( But please bear in mind that I am NOT a Ruby pro or expert, so this is an amateur's opinion. ) So for example in most languages if you have an array or list like this:
myList = [1, 2, 3, 4, 5] or [1; 2; 3; 4; 5] or you get the idea, and let's say I do this: myList[10] or ( List.nth myList 10 ) or something to that effect, I will get back an error! Ruby has no ArrayIndexOutOfBoundsException. If I do that in Ruby, I'll just get nil. ( Ruby's version of null. ) I know from experience that C++, Java, Python, and Ocaml will return an error message. But Ruby permits this and just returns nil. If the user is prompted for integer input in Ruby, and the user enters "Hello World" ( which certainly is not an integer! ) then the string gets converted into the integer 0 if you call the to_i method. I found this to be rather annoying when I wrote this program where I wanted to validate the user's input for correctness. "Hello World" is not valid input! But Ruby just passed the string along into my integer array, but the string got converted into 0, which is the default integer value in Ruby. So Ruby is very, very forgiving. That's great if you're new to programming. But that forgiving nature can lead to some issues if you want to validate user input, etc. But again.... I'm no Ruby pro! I'm sure that a professional Rubyist would have some way to address this particular issue.
On Tue, Mar 29, 2016 at 10:11 AM, Kenneth Miller [email protected] [ocaml_beginners] <[email protected]> wrote:
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.
#yiv5275489829 #yiv5275489829 -- #yiv5275489829ygrp-mkp {border:1px solid #d8d8d8;font-family:Arial;margin:10px 0;padding:0 10px;}#yiv5275489829 #yiv5275489829ygrp-mkp hr {border:1px solid #d8d8d8;}#yiv5275489829 #yiv5275489829ygrp-mkp #yiv5275489829hd {color:#628c2a;font-size:85%;font-weight:700;line-height:122%;margin:10px 0;}#yiv5275489829 #yiv5275489829ygrp-mkp #yiv5275489829ads {margin-bottom:10px;}#yiv5275489829 #yiv5275489829ygrp-mkp .yiv5275489829ad {padding:0 0;}#yiv5275489829 #yiv5275489829ygrp-mkp .yiv5275489829ad p {margin:0;}#yiv5275489829 #yiv5275489829ygrp-mkp .yiv5275489829ad a {color:#0000ff;text-decoration:none;}#yiv5275489829 #yiv5275489829ygrp-sponsor #yiv5275489829ygrp-lc {font-family:Arial;}#yiv5275489829 #yiv5275489829ygrp-sponsor #yiv5275489829ygrp-lc #yiv5275489829hd {margin:10px 0px;font-weight:700;font-size:78%;line-height:122%;}#yiv5275489829 #yiv5275489829ygrp-sponsor #yiv5275489829ygrp-lc .yiv5275489829ad {margin-bottom:10px;padding:0 0;}#yiv5275489829 #yiv5275489829actions {font-family:Verdana;font-size:11px;padding:10px 0;}#yiv5275489829 #yiv5275489829activity {background-color:#e0ecee;float:left;font-family:Verdana;font-size:10px;padding:10px;}#yiv5275489829 #yiv5275489829activity span {font-weight:700;}#yiv5275489829 #yiv5275489829activity span:first-child {text-transform:uppercase;}#yiv5275489829 #yiv5275489829activity span a {color:#5085b6;text-decoration:none;}#yiv5275489829 #yiv5275489829activity span span {color:#ff7900;}#yiv5275489829 #yiv5275489829activity span .yiv5275489829underline {text-decoration:underline;}#yiv5275489829 .yiv5275489829attach {clear:both;display:table;font-family:Arial;font-size:12px;padding:10px 0;width:400px;}#yiv5275489829 .yiv5275489829attach div a {text-decoration:none;}#yiv5275489829 .yiv5275489829attach img {border:none;padding-right:5px;}#yiv5275489829 .yiv5275489829attach label {display:block;margin-bottom:5px;}#yiv5275489829 .yiv5275489829attach label a {text-decoration:none;}#yiv5275489829 blockquote {margin:0 0 0 4px;}#yiv5275489829 .yiv5275489829bold {font-family:Arial;font-size:13px;font-weight:700;}#yiv5275489829 .yiv5275489829bold a {text-decoration:none;}#yiv5275489829 dd.yiv5275489829last p a {font-family:Verdana;font-weight:700;}#yiv5275489829 dd.yiv5275489829last p span {margin-right:10px;font-family:Verdana;font-weight:700;}#yiv5275489829 dd.yiv5275489829last p span.yiv5275489829yshortcuts {margin-right:0;}#yiv5275489829 div.yiv5275489829attach-table div div a {text-decoration:none;}#yiv5275489829 div.yiv5275489829attach-table {width:400px;}#yiv5275489829 div.yiv5275489829file-title a, #yiv5275489829 div.yiv5275489829file-title a:active, #yiv5275489829 div.yiv5275489829file-title a:hover, #yiv5275489829 div.yiv5275489829file-title a:visited {text-decoration:none;}#yiv5275489829 div.yiv5275489829photo-title a, #yiv5275489829 div.yiv5275489829photo-title a:active, #yiv5275489829 div.yiv5275489829photo-title a:hover, #yiv5275489829 div.yiv5275489829photo-title a:visited {text-decoration:none;}#yiv5275489829 div#yiv5275489829ygrp-mlmsg #yiv5275489829ygrp-msg p a span.yiv5275489829yshortcuts {font-family:Verdana;font-size:10px;font-weight:normal;}#yiv5275489829 .yiv5275489829green {color:#628c2a;}#yiv5275489829 .yiv5275489829MsoNormal {margin:0 0 0 0;}#yiv5275489829 o {font-size:0;}#yiv5275489829 #yiv5275489829photos div {float:left;width:72px;}#yiv5275489829 #yiv5275489829photos div div {border:1px solid #666666;height:62px;overflow:hidden;width:62px;}#yiv5275489829 #yiv5275489829photos div label {color:#666666;font-size:10px;overflow:hidden;text-align:center;white-space:nowrap;width:64px;}#yiv5275489829 #yiv5275489829reco-category {font-size:77%;}#yiv5275489829 #yiv5275489829reco-desc {font-size:77%;}#yiv5275489829 .yiv5275489829replbq {margin:4px;}#yiv5275489829 #yiv5275489829ygrp-actbar div a:first-child {margin-right:2px;padding-right:5px;}#yiv5275489829 #yiv5275489829ygrp-mlmsg {font-size:13px;font-family:Arial, helvetica, clean, sans-serif;}#yiv5275489829 #yiv5275489829ygrp-mlmsg table {font-size:inherit;font:100%;}#yiv5275489829 #yiv5275489829ygrp-mlmsg select, #yiv5275489829 input, #yiv5275489829 textarea {font:99% Arial, Helvetica, clean, sans-serif;}#yiv5275489829 #yiv5275489829ygrp-mlmsg pre, #yiv5275489829 code {font:115% monospace;}#yiv5275489829 #yiv5275489829ygrp-mlmsg * {line-height:1.22em;}#yiv5275489829 #yiv5275489829ygrp-mlmsg #yiv5275489829logo {padding-bottom:10px;}#yiv5275489829 #yiv5275489829ygrp-msg p a {font-family:Verdana;}#yiv5275489829 #yiv5275489829ygrp-msg p#yiv5275489829attach-count span {color:#1E66AE;font-weight:700;}#yiv5275489829 #yiv5275489829ygrp-reco #yiv5275489829reco-head {color:#ff7900;font-weight:700;}#yiv5275489829 #yiv5275489829ygrp-reco {margin-bottom:20px;padding:0px;}#yiv5275489829 #yiv5275489829ygrp-sponsor #yiv5275489829ov li a {font-size:130%;text-decoration:none;}#yiv5275489829 #yiv5275489829ygrp-sponsor #yiv5275489829ov li {font-size:77%;list-style-type:square;padding:6px 0;}#yiv5275489829 #yiv5275489829ygrp-sponsor #yiv5275489829ov ul {margin:0;padding:0 0 0 8px;}#yiv5275489829 #yiv5275489829ygrp-text {font-family:Georgia;}#yiv5275489829 #yiv5275489829ygrp-text p {margin:0 0 1em 0;}#yiv5275489829 #yiv5275489829ygrp-text tt {font-size:120%;}#yiv5275489829 #yiv5275489829ygrp-vital ul li:last-child {border-right:none !important;}#yiv5275489829