Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: John Prevost <prevost@maya.com>
To: Pierre Weis <Pierre.Weis@inria.fr>
Cc: skaller@maxtal.com.au (John Skaller), caml-list@inria.fr
Subject: Re: convincing management to switch to Ocaml
Date: 30 Aug 1999 20:13:57 -0400	[thread overview]
Message-ID: <m3vh9wolh6.fsf@isil.maya.com> (raw)
In-Reply-To: Pierre Weis's message of "Mon, 30 Aug 1999 10:02:52 +0200 (MET DST)"

Pierre Weis <Pierre.Weis@inria.fr> writes:

> I don't want to start a flamewar on syntax, but as a computer science
> teacher and Caml implementor and designer, I'm interested at those
> facts you mentioned about the syntax of the language, since I just
> think the opposite way: I find the Caml precedence rules pretty
> convenient, easy to teach, and fairly easy to remember since
> absolutely intuitive and natural (provided they have been explained to
> you and you have understood the design ideas).
> 
> So, there is something interesting to understand here, could you
> elaborate a bit on your difficulties on precedences and especially
> about ``all the brackets'' that make the code hard to read and hard to
> write ?

I had a similar problem with SML and Caml until I realized the one
important rule that makes things clear: The juxtaposition
(application) operator has the highest precedence.  There are two
places this can hurt you, if you don't understand it yet:

let f_of_sum = f x + y
vs
let f_of_sum = f (x + y)

This one's actually pretty simple to figure out--it doesn't take long,
though people tend to feel unsure and write:

let sum_of_f = (f x) + (f y)

constructions when they don't have to.

(And of course, C programmers get bitten by wanting function
application to look like f(arg), rather than f arg.  It doesn't help
that oftentimes intro material takes this tack, then moves away from
it, leaving lingering uncertainties about f(x) in the student's mind.)

The other problem, which is more significant (although it causes more
problems in SML) is constructors.  When I first learned ML, it wasn't
made obvious to me that the application of a value constructor is
exactly the same as any other function.  In fact, it seemed that it
shouldn't be, since you can do pattern matching on one and not on the
other.

The painful part of this shows up mainly in patterns like the following:

let f = fun (Foo x) (Bar y) (Baz z) -> ...

It's not so much of a problem in Caml, since the above sort of pattern
is rarely useful, but in SML, where using multiple cases and multiple
patterns at the same time is possible, it makes for confusing
behavior:

fun f (Int a) (Int b) = Int (a + b)
  | f (Int a) (Flt b) = Flt (float a + b)
  | f (Flt a) (Int b) = Flt (a + float b)
  | f (Flt a) (Flt b) = Flt (a + b)

When I first tried to do something like the above, my first mistake
was to try something like this:

fun f Int(a) Int(b) = Int (a + b)

But this doesn't work, of course.  I then tried various things, until
I got:

fun f (Int a) (Int b) = Int (a + b)

At this point, I decided "Oh, I'll just write everything I can like
Lisp!" and tried to do the following for consistency:

fun f (Int a) (Int b) = (Int a + b)

Which of course, doesn't work.


Summary: It's important for introductory material to point out that 
" " is an operator, similar to others, and how it works precedence-wise.
It's also important to point out that constructor application in both
expressions and patterns acts exactly the same as function
application.  This makes things much clearer.  You can then also point
out useful things like the fact that , binds less tightly than
anything else, so you can say (long expression, long expression) to
form a tuple., and that similar rules apply to other things.

Juxtaposition as application is in some ways the most foreign bit of
syntax for a C-style language programmer.


John.




  parent reply	other threads:[~1999-08-31 17:14 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-07-28 14:47 STARYNKEVITCH Basile
1999-07-30  9:00 ` Markus Mottl
1999-08-13 10:32   ` John Skaller
1999-08-25  1:51     ` Frank A. Christoph
1999-08-25  3:50       ` John Skaller
1999-08-25  6:34         ` Frank A. Christoph
1999-08-26 18:36         ` Stefan Monnier
1999-08-29  6:08           ` John Skaller
1999-08-27 10:00         ` Andreas Rossberg
1999-08-28  6:24           ` John Skaller
1999-08-30 15:59             ` Sylvain BOULM'E
1999-08-31  5:50             ` Brian Rogoff
1999-08-28 19:51           ` Dave Mason
1999-08-30 19:05             ` Xavier Leroy
1999-08-30  8:02           ` Pierre Weis
1999-08-30 19:35             ` John Skaller
1999-08-31 17:10               ` Pierre Weis
1999-09-03  6:56                 ` John Skaller
1999-08-31 19:03               ` Stefan Monnier
1999-09-03  7:28                 ` John Skaller
1999-08-31  0:13             ` John Prevost [this message]
1999-08-31  5:19               ` John Skaller
1999-08-31  6:35                 ` John Prevost
1999-09-03  5:42                   ` John Skaller
1999-08-31 16:24           ` Gerard Huet
1999-07-30 14:42 ` John Skaller
1999-07-30 18:49 ` Gerd Stolpmann
1999-07-30 21:30 ` Francois Rouaix
1999-08-12 10:36 ` Reply to: " Jens Olsson
1999-08-16 18:33   ` Chris Tilt
1999-08-12 12:15 ` Frank A. Christoph
1999-08-15  8:14   ` Friedman Roy
  -- strict thread matches above, loose matches on Subject: below --
1999-09-07  7:24 TommyHallgren
     [not found] <John Skaller's message of "Tue, 31 Aug 1999 15:19:48 +1000">

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m3vh9wolh6.fsf@isil.maya.com \
    --to=prevost@maya.com \
    --cc=Pierre.Weis@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=skaller@maxtal.com.au \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox