caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Frustrated Beginner
@ 2003-12-23  4:03 Tyler Eaves
  2003-12-23  4:19 ` jayanta nath
                   ` (6 more replies)
  0 siblings, 7 replies; 37+ messages in thread
From: Tyler Eaves @ 2003-12-23  4:03 UTC (permalink / raw)
  To: caml-list

First a bit of background:

I'm a 19 yr old computer science student, been programming for perhaps
10 years. Started (Like so many did in the early '90s with QBasic in
DOS. These days most of my work is done in either PHP (web stuff) or
Python (everything else) with the occasional bit of C for stuff that
needs to run really fast. I'm also passingly familiar with a number of
other langauges (C++, Java, Perl, etc). 

So why is O'Caml giving me so much trouble? I've been trying to pick it
up for about a week now, read various online tutorials. I'm just having
no luck at all. My largest program to date (that works) is all of 3
lines long, and simply printed out the command line arguments passed to
the program.

My biggest source of problems seems to be the syntax. I'm totally
confused as far as ; vs ;; vs nothing, when to use ( ), and things of
the like. It doesn't help that the compiler is completly naive when it
comes to Syntax Errors. It would be so helpful if it could give an error
message that actually told the programmer what it expected. I realise
that O'Camls syntax allows many things, so that it may be hard to say
exactly what it WAS expecting in all cases, but surely SOMETHING
meaningful, besides a charater postion could be given? 

What I'd really like is a site with examples of actual programs. Most of
the example code I've seen is A: Not commened and B: Recursive
Mathematical functions. I'd really love to see an example, of say,
reading in a file, looking at each character, and doing something when
it encounters, say, a tab. Or any other such program that actually does
something. 

I really want to like O'Caml. It seems to offer a very nice feature set,
and the ability to compile to super-fast native code is the icing on the
cake. Right now though, I'm frustrated and on the verge of giving up.

TE


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23  4:03 [Caml-list] Frustrated Beginner Tyler Eaves
@ 2003-12-23  4:19 ` jayanta nath
  2003-12-23  5:34   ` Matt Gushee
  2003-12-23  5:58 ` Dustin Sallings
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 37+ messages in thread
From: jayanta nath @ 2003-12-23  4:19 UTC (permalink / raw)
  To: tyler; +Cc: caml-list

Hello Tyler Eaves,

Nice to know about you!

Error reporting is a big issue not just in OCaml but in any functional
language. A great deal of research is currently going on to better the
error reporting. As for a site where you can learn OCaml, to the best of
my knowledge, the online manual for OCaml version 3.07 at the site of
INRIA (the institute where OCaml has been developed) might be a good
starting point for you. The link is :

http://caml.inria.fr/ocaml/htmlman/

Other than this, I think it will take some time and practice to get used
to the syntactical issues like ";;" for ";".

Best of Luck in learning OCaml!

Regards,
Jayanta Kumar Nath.

> First a bit of background:
>
> I'm a 19 yr old computer science student, been programming for perhaps
> 10 years. Started (Like so many did in the early '90s with QBasic in
> DOS. These days most of my work is done in either PHP (web stuff) or
> Python (everything else) with the occasional bit of C for stuff that
> needs to run really fast. I'm also passingly familiar with a number of
> other langauges (C++, Java, Perl, etc).
>
> So why is O'Caml giving me so much trouble? I've been trying to pick it
> up for about a week now, read various online tutorials. I'm just having
> no luck at all. My largest program to date (that works) is all of 3
> lines long, and simply printed out the command line arguments passed to
> the program.
>
> My biggest source of problems seems to be the syntax. I'm totally
> confused as far as ; vs ;; vs nothing, when to use ( ), and things of
> the like. It doesn't help that the compiler is completly naive when it
> comes to Syntax Errors. It would be so helpful if it could give an error
> message that actually told the programmer what it expected. I realise
> that O'Camls syntax allows many things, so that it may be hard to say
> exactly what it WAS expecting in all cases, but surely SOMETHING
> meaningful, besides a charater postion could be given?
>
> What I'd really like is a site with examples of actual programs. Most of
> the example code I've seen is A: Not commened and B: Recursive
> Mathematical functions. I'd really love to see an example, of say,
> reading in a file, looking at each character, and doing something when
> it encounters, say, a tab. Or any other such program that actually does
> something.
>
> I really want to like O'Caml. It seems to offer a very nice feature set,
> and the ability to compile to super-fast native code is the icing on the
> cake. Right now though, I'm frustrated and on the verge of giving up.
>
> TE
>
>
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives:
> http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs
> FAQ: http://caml.inria.fr/FAQ/ Beginner's list:
> http://groups.yahoo.com/group/ocaml_beginners



-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23  4:19 ` jayanta nath
@ 2003-12-23  5:34   ` Matt Gushee
  2003-12-23  6:11     ` Tyler Eaves
  0 siblings, 1 reply; 37+ messages in thread
From: Matt Gushee @ 2003-12-23  5:34 UTC (permalink / raw)
  To: caml-list

On Mon, Dec 22, 2003 at 11:03:07PM -0500, Tyler Eaves wrote:
> 
> I'm a 19 yr old computer science student, been programming for perhaps
> 10 years. ... 

Welcome! I'm at best an intermediate OCaml programmer, but I'll try to
make a helpful suggestion or two.

> So why is O'Caml giving me so much trouble?

Do you have any previous experience with functional programming (FP), or
have you studied FP in school? If not, then you're learning both a new
syntax and a very different programming paradigm at once, so you should
expect a steep learning curve. But I think if you stick with it, after a
while it will all start to make sense, and you'll be glad you made the
effort.

> I've been trying to pick it up for about a week now, read various
> online tutorials.

Which ones? Personally, I have found David Matuszek's and Richard Jones'
tutorials to be the most helpful for learning fundamentals.

  http://www.csc.vill.edu/~dmatusze/resources/ocaml/ocaml.html
  http://www.merjis.com/richj/computers/ocaml/tutorial/

> My biggest source of problems seems to be the syntax. I'm totally
> confused as far as ; vs ;; vs nothing, ...

Yes, that is a bit tricky. You've undoubtedly read explanations of the
semicolons, but sometimes it helps if you get the same information again
in slightly different terms, so let me try:

  * A double semicolon ends a "sentence"--that is, it terminates a
    top-level construct such as a function definition--but not nested
    function definitions, because they're not "sentences," they're
    "phrases". 
    
    You can omit ;; in most cases, but I would suggest at first using 
    it everywhere it is allowed. When you omit the ;; and there is a 
    syntax error in your code, the compiler often goes many lines past
    the real trouble spot before it detects an error, so using ;;
    everywhere can narrow down your search.

  * The single semicolon is perhaps a bit harder to understand, but I
    think it helps to keep in mind that OCaml is basically a functional
    language, yet it also supports imperative programming. Being
    functional means that there are no statements per se. It's all about
    evaluating expressions, and *every* expression returns a value.
    However, there are expressions that are functionally equivalent to
    statements. In order to conform to the functional model, they have
    to return a value, but there is no useful value to return. So they
    return the unit value, '()'.

    Basically, whenever you are programming imperatively--when you have
    one of these pseudo-statements that return (), and when it this
    imperative phrase is not the final result of a function, you need to
    separate it from the following phrase with a single semicolon. A
    simple example:

      match foo with
      | None -> print_endline "Nothing"; ""
      | Some x -> print_endline x; x

    This expression returns a string, but before returning, you want to
    print a message. Printing functions, of course, return ().

Hope this helps a bit. Best of luck to you!
    
-- 
Matt Gushee                 When a nation follows the Way,
Englewood, Colorado, USA    Horses bear manure through
mgushee@havenrock.com           its fields;
http://www.havenrock.com/   When a nation ignores the Way,
                            Horses bear soldiers through
                                its streets.
                                
                            --Lao Tzu (Peter Merel, trans.)

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23  4:03 [Caml-list] Frustrated Beginner Tyler Eaves
  2003-12-23  4:19 ` jayanta nath
@ 2003-12-23  5:58 ` Dustin Sallings
       [not found]   ` <EAEE2FF2-3510-11D8-B3A1-000A9584A16E@ml1.net>
  2003-12-23  6:20 ` Tom Murray
                   ` (4 subsequent siblings)
  6 siblings, 1 reply; 37+ messages in thread
From: Dustin Sallings @ 2003-12-23  5:58 UTC (permalink / raw)
  To: Tyler Eaves; +Cc: caml-list


On Dec 22, 2003, at 20:03, Tyler Eaves wrote:

> I'm a 19 yr old computer science student, been programming for perhaps
> 10 years. Started (Like so many did in the early '90s with QBasic in
> DOS. These days most of my work is done in either PHP (web stuff) or
> Python (everything else) with the occasional bit of C for stuff that
> needs to run really fast. I'm also passingly familiar with a number of
> other langauges (C++, Java, Perl, etc).
>
> So why is O'Caml giving me so much trouble? I've been trying to pick it
> up for about a week now, read various online tutorials. I'm just having
> no luck at all. My largest program to date (that works) is all of 3
> lines long, and simply printed out the command line arguments passed to
> the program.

	Well, lack of variety, honestly.  C++, Java, and C are all pretty much 
the same language in the grand scheme of things.  Perl means a lot of 
different things to a lot of different people.  Python is also not that 
much different conceptually, just a slight change in syntax.

	Ocaml is a bit strange in a lot of ways, but it doesn't really take 
that long to get used to.  Like anything else, you just need to find a 
problem and solve it with the language.  I had a pretty large mental 
workout last night writing a little program to solve one of those 
monoalphabetic random substitution ciphers.   A lot of it was helping 
me figure out more areas of the syntax I've not explored.

	Functional programming in general is a lot different from just about 
anything you've done in any of the languages you've mentioned.  You 
might find yourself rarely using variables, having side-effects 
extremely rarely, and doing a lot of stuff with higher order functions 
in general (which you can do to a degree in python and perl).  Oh, and 
get comfortable with recursion.  Most functional languages are tail 
recursive and it's the preferred method for looping.  Some languages 
(like OCaml) have imperative loop constructs, but they could just as 
easily have been left out.

> My biggest source of problems seems to be the syntax. I'm totally
> confused as far as ; vs ;; vs nothing, when to use ( ), and things of
> the like. It doesn't help that the compiler is completly naive when it
> comes to Syntax Errors. It would be so helpful if it could give an 
> error
> message that actually told the programmer what it expected. I realise
> that O'Camls syntax allows many things, so that it may be hard to say
> exactly what it WAS expecting in all cases, but surely SOMETHING
> meaningful, besides a charater postion could be given?

	; is when you're writing more imperative code.  In an imperative 
style, you give the computer a list of things to do and tell it the 
order in which to do things.  That's not so much the case in a 
functional style.

	;; is used to end a definition altogether.

	Parens are interesting.  They're generally not required unless you 
want to contain something into being one argument.  I use them a lot 
for clarity, but you typically do not want to use them for function 
calls.  This is mainly because currying (which probably won't make 
sense to you until you've been using ocaml or haskell or something else 
that supports it for a little while) makes a lot of things better.  :)

	Overall, once you get more comfortable with the syntax, it won't be so 
bad.

> What I'd really like is a site with examples of actual programs. Most 
> of
> the example code I've seen is A: Not commened and B: Recursive
> Mathematical functions. I'd really love to see an example, of say,
> reading in a file, looking at each character, and doing something when
> it encounters, say, a tab. Or any other such program that actually does
> something.

	You should expect to see a lot of recursion.

	One possible solution for the example you're requesting might look 
like this:

let rec loop s = (
     match Stream.next s with
           '\t' -> print_string(" - tab - ")
         | c -> print_char(c)
     );
     loop s
;;

let f = open_in Sys.argv.(1) in
try
     (* Loop on this stream *)
     loop (Stream.of_channel f);
     (* The previous statement had a sem because it's an imperative 
style here *)
     close_in f
with Stream.Failure ->
     (* Occurs at the end of the stream *)
     close_in f;

	That's not exactly how *I'd* do it because I've got file processing 
functions I use that handle all of the failures and stuff for me, but 
it's an example.  There are other errors that my occur during your file 
that this won't handle (making sure your fd gets closed).  I probably 
also wouldn't have loop as a separate function (actually, I pulled it 
out for this example).  It's close enough, though.

	You're welcome to read through any of my code (you'll need tla or 
another arch system installed).  Not all of it's well commented, but it 
certainly does stuff.  :)

> I really want to like O'Caml. It seems to offer a very nice feature 
> set,
> and the ability to compile to super-fast native code is the icing on 
> the
> cake. Right now though, I'm frustrated and on the verge of giving up.

	I don't believe it's the easiest FP language to learn.  Certainly 
scheme has a very simple syntax (though no currying, which I think does 
actually make things easier once you get going).  I've been using it 
quite a bit for day-to-day work just because it's so amazingly fast and 
works consistently.  I had a scheme compiler I was using for a while 
that would sometimes cause me problems (implementation bugs) and wasn't 
nearly as fast.  I'm glad I finally came around to trying ocaml again.

--
SPY                      My girlfriend asked me which one I like better.
pub  1024/3CAE01D5 1994/11/03 Dustin Sallings <dustin@spy.net>
|    Key fingerprint =  87 02 57 08 02 D0 DA D6  C8 0F 3E 65 51 98 D8 BE
L_______________________ I hope the answer won't upset her. ____________

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23  5:34   ` Matt Gushee
@ 2003-12-23  6:11     ` Tyler Eaves
  2003-12-23  6:21       ` Michael Vanier
                         ` (3 more replies)
  0 siblings, 4 replies; 37+ messages in thread
From: Tyler Eaves @ 2003-12-23  6:11 UTC (permalink / raw)
  To: Matt Gushee; +Cc: caml-list


On Dec 23, 2003, at 12:34 AM, Matt Gushee wrote:
>
>> So why is O'Caml giving me so much trouble?
>
> Do you have any previous experience with functional programming (FP), 
> or
> have you studied FP in school? If not, then you're learning both a new
> syntax and a very different programming paradigm at once, so you should
> expect a steep learning curve. But I think if you stick with it, after 
> a
> while it will all start to make sense, and you'll be glad you made the
> effort.

Not much, a (very) little Lisp.  Not really enough to 'get it'.
>
>> I've been trying to pick it up for about a week now, read various
>> online tutorials.
>

>  http://www.merjis.com/richj/computers/ocaml/tutorial/
This is the one I've gotten the most out of.

>> My biggest source of problems seems to be the syntax. I'm totally
>> confused as far as ; vs ;; vs nothing, ...
>
> Yes, that is a bit tricky. You've undoubtedly read explanations of the
> semicolons, but sometimes it helps if you get the same information 
> again
> in slightly different terms, so let me try:
>
>   * A double semicolon ends a "sentence"--that is, it terminates a
>     top-level construct such as a function definition--but not nested
>     function definitions, because they're not "sentences," they're
>     "phrases".
>
>     You can omit ;; in most cases, but I would suggest at first using
>     it everywhere it is allowed. When you omit the ;; and there is a
>     syntax error in your code, the compiler often goes many lines past
>     the real trouble spot before it detects an error, so using ;;
>     everywhere can narrow down your search.
>
>   * The single semicolon is perhaps a bit harder to understand, but I
>     think it helps to keep in mind that OCaml is basically a functional
>     language, yet it also supports imperative programming. Being
>     functional means that there are no statements per se. It's all 
> about
>     evaluating expressions, and *every* expression returns a value.
>     However, there are expressions that are functionally equivalent to
>     statements. In order to conform to the functional model, they have
>     to return a value, but there is no useful value to return. So they
>     return the unit value, '()'.

Okay, that helps.

>     Basically, whenever you are programming imperatively--when you have
>     one of these pseudo-statements that return (), and when it this
>     imperative phrase is not the final result of a function, you need 
> to
>     separate it from the following phrase with a single semicolon. A
>     simple example:
>
>       match foo with
>       | None -> print_endline "Nothing"; ""
>       | Some x -> print_endline x; x

If I understand the match syntax correctly, in this case, x takes the 
value of foo?
>     This expression returns a string, but before returning, you want to
>     print a message. Printing functions, of course, return ().
>
> Hope this helps a bit. Best of luck to you

It does, quite a bit. Thanks to everyone else too, I've already gotten 
something like 11 replies in an hour and a half :)

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23  4:03 [Caml-list] Frustrated Beginner Tyler Eaves
  2003-12-23  4:19 ` jayanta nath
  2003-12-23  5:58 ` Dustin Sallings
@ 2003-12-23  6:20 ` Tom Murray
  2003-12-23  8:52 ` Stefano Zacchiroli
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 37+ messages in thread
From: Tom Murray @ 2003-12-23  6:20 UTC (permalink / raw)
  To: Tyler Eaves; +Cc: caml-list

As a programmer who did imperative and OO for 15+ years, and then 
switched to OCaml this year, I encourage you to stick with it. I think 
it will improve how you write in other languages, too (and Python has 
many functional features, btw).

I agree that the syntax can be frightful, but it seems to be a 
requirement of functional languages.

Some things that helped me:

* Do lots of experimenting in the interactive top-level. If you're an 
Emacs user, get the Tuareg mode. The nice thing about working in Emacs 
is that you can evaluate an expression or two in your file, see what 
happens, make some changes to the expression, repeat.

* I found the O'Reilly book very helpful. It doesn't assume familiarity 
with the functional way of doing things, like many other write-ups do. 
It's very application-oriented, which will make the examples familiar 
to you. And it has separate chapters for functional, imperative, and 
OO, so you get a feel for the different ways of writing OCaml code. You 
can download the book from http://caml.inria.fr/oreilly-book/.

Good luck,

tm

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23  6:11     ` Tyler Eaves
@ 2003-12-23  6:21       ` Michael Vanier
  2003-12-23  6:31       ` Michael Jeffrey Tucker
                         ` (2 subsequent siblings)
  3 siblings, 0 replies; 37+ messages in thread
From: Michael Vanier @ 2003-12-23  6:21 UTC (permalink / raw)
  To: tyler; +Cc: mgushee, caml-list


To learn the functional style of programming I strongly recommend Abelson
and Sussman's Structure and Interpretation of Computer Programs.  Even
though it uses scheme, not ocaml, the functional style easily transfers
from one language to another.

Mike

> Cc: caml-list@inria.fr
> From: Tyler Eaves <tyler@ml1.net>
> Date: Tue, 23 Dec 2003 01:11:08 -0500
> 
> 
> On Dec 23, 2003, at 12:34 AM, Matt Gushee wrote:
> >
> >> So why is O'Caml giving me so much trouble?
> >
> > Do you have any previous experience with functional programming (FP), 
> > or
> > have you studied FP in school? If not, then you're learning both a new
> > syntax and a very different programming paradigm at once, so you should
> > expect a steep learning curve. But I think if you stick with it, after 
> > a
> > while it will all start to make sense, and you'll be glad you made the
> > effort.
> 
> Not much, a (very) little Lisp.  Not really enough to 'get it'.
> >
> >> I've been trying to pick it up for about a week now, read various
> >> online tutorials.
> >
> 
> >  http://www.merjis.com/richj/computers/ocaml/tutorial/
> This is the one I've gotten the most out of.
> 
> >> My biggest source of problems seems to be the syntax. I'm totally
> >> confused as far as ; vs ;; vs nothing, ...
> >
> > Yes, that is a bit tricky. You've undoubtedly read explanations of the
> > semicolons, but sometimes it helps if you get the same information 
> > again
> > in slightly different terms, so let me try:
> >
> >   * A double semicolon ends a "sentence"--that is, it terminates a
> >     top-level construct such as a function definition--but not nested
> >     function definitions, because they're not "sentences," they're
> >     "phrases".
> >
> >     You can omit ;; in most cases, but I would suggest at first using
> >     it everywhere it is allowed. When you omit the ;; and there is a
> >     syntax error in your code, the compiler often goes many lines past
> >     the real trouble spot before it detects an error, so using ;;
> >     everywhere can narrow down your search.
> >
> >   * The single semicolon is perhaps a bit harder to understand, but I
> >     think it helps to keep in mind that OCaml is basically a functional
> >     language, yet it also supports imperative programming. Being
> >     functional means that there are no statements per se. It's all 
> > about
> >     evaluating expressions, and *every* expression returns a value.
> >     However, there are expressions that are functionally equivalent to
> >     statements. In order to conform to the functional model, they have
> >     to return a value, but there is no useful value to return. So they
> >     return the unit value, '()'.
> 
> Okay, that helps.
> 
> >     Basically, whenever you are programming imperatively--when you have
> >     one of these pseudo-statements that return (), and when it this
> >     imperative phrase is not the final result of a function, you need 
> > to
> >     separate it from the following phrase with a single semicolon. A
> >     simple example:
> >
> >       match foo with
> >       | None -> print_endline "Nothing"; ""
> >       | Some x -> print_endline x; x
> 
> If I understand the match syntax correctly, in this case, x takes the 
> value of foo?
> >     This expression returns a string, but before returning, you want to
> >     print a message. Printing functions, of course, return ().
> >
> > Hope this helps a bit. Best of luck to you
> 
> It does, quite a bit. Thanks to everyone else too, I've already gotten 
> something like 11 replies in an hour and a half :)
> 
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> 

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23  6:11     ` Tyler Eaves
  2003-12-23  6:21       ` Michael Vanier
@ 2003-12-23  6:31       ` Michael Jeffrey Tucker
  2003-12-23 12:16         ` Richard Jones
  2003-12-23  6:32       ` Shawn Wagner
  2003-12-23  6:43       ` Matt Gushee
  3 siblings, 1 reply; 37+ messages in thread
From: Michael Jeffrey Tucker @ 2003-12-23  6:31 UTC (permalink / raw)
  To: Tyler Eaves; +Cc: Matt Gushee, caml-list

Hi Tyler,

> >       match foo with
> >       | None -> print_endline "Nothing"; ""
> >       | Some x -> print_endline x; x
>
> If I understand the match syntax correctly, in this case, x takes the
> value of foo?

You're pretty close - in this case, you can think of foo as being a
structure that contains a tag and potentially a valid value. In C this
might be represented by:

struct foo_type {
	enum {Some, None} opt;
	value_t val;
};

If the opt field of the structure is set to Some, then you are free to
look at the val field. If it's none, then the value in the val field is
unspecified and should be ignored (of course this is more stringently
enforced in OCaml). In OCaml an "option type" variable has either the
value "Some xxx" or "None" where xxx is a value. So, a "string option"
variable might have the value Some "hi", Some "qwery" or None. If you
pattern match on a variable of this type, as in:

match foo with
| None -> "Argh! No value."
| Some x -> x

Then, you are creating a new binding for x, where x will be assigned the
value portion. So, in the example values above, this code will evaluate
to (in order):

"hi"
"qwery"
"Argh! No value."

Hope that helps,
Mike

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23  6:11     ` Tyler Eaves
  2003-12-23  6:21       ` Michael Vanier
  2003-12-23  6:31       ` Michael Jeffrey Tucker
@ 2003-12-23  6:32       ` Shawn Wagner
  2003-12-23  6:43       ` Matt Gushee
  3 siblings, 0 replies; 37+ messages in thread
From: Shawn Wagner @ 2003-12-23  6:32 UTC (permalink / raw)
  To: caml-list

On Tue, Dec 23, 2003 at 01:11:08AM -0500, Tyler Eaves wrote:
> 
> On Dec 23, 2003, at 12:34 AM, Matt Gushee wrote:
> >
> >      match foo with
> >      | None -> print_endline "Nothing"; ""
> >      | Some x -> print_endline x; x
> 
> If I understand the match syntax correctly, in this case, x takes the 
> value of foo?

Not quite. foo is an option type - it can hold either Some valid bit of data
(A string in this case), or nothing (None). x ends up taking the value of
whatever the valid bit of data is. The same thing in C would look like

if (foo) {
   puts(foo);
   return foo;
} else {
   puts("Nothing");
   return "";
}

Options are nicer, however, because you don't have the problem that crops up
in C of not handling cases where NULL is a valid bit of data because it's
treated as 'No data'. Hash lookup functions that return NULL when a key's
not found are a classic example. What happens when you want to store NULL in
one?

-- 
Shawn Wagner
shawnw@speakeasy.org

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23  6:11     ` Tyler Eaves
                         ` (2 preceding siblings ...)
  2003-12-23  6:32       ` Shawn Wagner
@ 2003-12-23  6:43       ` Matt Gushee
  3 siblings, 0 replies; 37+ messages in thread
From: Matt Gushee @ 2003-12-23  6:43 UTC (permalink / raw)
  To: caml-list

On Tue, Dec 23, 2003 at 01:11:08AM -0500, Tyler Eaves wrote:

> >      match foo with
> >      | None -> print_endline "Nothing"; ""
> >      | Some x -> print_endline x; x
> 
> If I understand the match syntax correctly, in this case, x takes the 
> value of foo?

Not quite. foo can have a value of either None or Some s, where s is an
arbitrary string. So if foo has a value of Some "abcde", then x =
"abcde". Foo, by the way, has a type of string option.

-- 
Matt Gushee                 When a nation follows the Way,
Englewood, Colorado, USA    Horses bear manure through
mgushee@havenrock.com           its fields;
http://www.havenrock.com/   When a nation ignores the Way,
                            Horses bear soldiers through
                                its streets.
                                
                            --Lao Tzu (Peter Merel, trans.)

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
       [not found]   ` <EAEE2FF2-3510-11D8-B3A1-000A9584A16E@ml1.net>
@ 2003-12-23  6:53     ` Dustin Sallings
  2003-12-23  7:23       ` Tyler Eaves
  0 siblings, 1 reply; 37+ messages in thread
From: Dustin Sallings @ 2003-12-23  6:53 UTC (permalink / raw)
  To: Tyler Eaves; +Cc: Caml List


On Dec 22, 2003, at 22:26, Tyler Eaves wrote:

(I'm sending this back to the list because, as you pointed out, there 
were a few problems with that example, so here's one for the archives).

> Okay, so far so good. I take it that in O'Caml the overhead of a 
> function call is much less than in, say, C? Otherwise this approach 
> would seem to be rather inefficient.

	Well, the short answer is that that's not a function call.  :)  See 
tail-recursion.

	It's also not the best example since it was so simple and didn't 
return anything useful.  Consider this implementation of fold (roughly 
translating it in this email from a scheme version I wrote on my palm):

let rec fold f init lst =
     match lst with
           [] -> init
         | h::t -> fold f (f init h) t
;;

	If you're not familiar with fold, here's an example:

fold (fun v c -> c + v) 0 [1;2;3;4;5;6;7;8;9;10]

	That invocation returns 55.  Basically, the function is called for 
each value in the list (v) and the current value is passed in along 
with the current value during the calculation (c).  When it gets to the 
end (list is []), you return the init value.  Otherwise, you apply the 
function.

	Note that this function could also be written as follows (and probably 
would be):

let rec fold f init = function
       [] -> init
     | h::t -> fold f (f init h) t
;;

> 	Would not Stream.iter be worth considering here? I found that when 
> looking at at the Stream doc.

	Absolutely.  I haven't typically used streams that way.  How 'bout a 
rewrite:

let f = open_in Sys.argv.(1) in
     (* Loop on this stream *)
     Stream.iter (function
         '\t' -> print_string " - tab - "
         | c -> print_char c
     ) (Stream.of_channel f);
     close_in f

>>     (* The previous statement had a sem because it's an imperative 
>> style here *)
>>     close_in f
> 	Will this close EVER be reached? I see only two possibilities, either 
> the stream eventually EOFs, or we've hit an 'infinite' file, such as 
> /dev/random

	Good catch. :)  I think that was another example of using streams a 
bit different from how I usually do, but bad examples are worse than no 
examples.  Or maybe it was one of those ``find the bug'' assignments.  
:)

--
SPY                      My girlfriend asked me which one I like better.
pub  1024/3CAE01D5 1994/11/03 Dustin Sallings <dustin@spy.net>
|    Key fingerprint =  87 02 57 08 02 D0 DA D6  C8 0F 3E 65 51 98 D8 BE
L_______________________ I hope the answer won't upset her. ____________

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23  6:53     ` Dustin Sallings
@ 2003-12-23  7:23       ` Tyler Eaves
  2003-12-23  8:26         ` Dustin Sallings
  0 siblings, 1 reply; 37+ messages in thread
From: Tyler Eaves @ 2003-12-23  7:23 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1857 bytes --]


On Dec 23, 2003, at 1:53 AM, Dustin Sallings wrote:

>
> On Dec 22, 2003, at 22:26, Tyler Eaves wrote:
>
> (I'm sending this back to the list because, as you pointed out, there 
> were a few problems with that example, so here's one for the 
> archives).
>
>> Okay, so far so good. I take it that in O'Caml the overhead of a 
>> function call is much less than in, say, C? Otherwise this approach 
>> would seem to be rather inefficient.
>
> 	Well, the short answer is that that's not a function call.  :)  See 
> tail-recursion.
>
> 	It's also not the best example since it was so simple and didn't 
> return anything useful.  Consider this implementation of fold (roughly 
> translating it in this email from a scheme version I wrote on my 
> palm):
>
> let rec fold f init lst =
>     match lst with
>           [] -> init
>         | h::t -> fold f (f init h) t
> ;;
>
> 	If you're not familiar with fold, here's an example:
>
> fold (fun v c -> c + v) 0 [1;2;3;4;5;6;7;8;9;10]
>
> 	That invocation returns 55.  Basically, the function is called for 
> each value in the list (v) and the current value is passed in along 
> with the current value during the calculation (c).  When it gets to 
> the end (list is []), you return the init value.  Otherwise, you apply 
> the function.
>
> 	Note that this function could also be written as follows (and 
> probably would be):
>
> let rec fold f init = function
>       [] -> init
>     | h::t -> fold f (f init h) t
> ;;

This makes since in general. I'm guess h and t correspond to the head 
and tail of the lift. It'll probably make more sense tomorrow. It's 
2:11 here, and I'd planned to be asleep two hours ago. The replies have 
kept me up ;)

Based on what I've learned, I've tried to write a simple prime number 
finder. It doesn't actually *work* or anything (Syntax Error on line 
23, can't figu

[-- Attachment #2: primes.ml --]
[-- Type: application/octet-stream, Size: 690 bytes --]

(*  primes.ml
    build a list of prime numbers.
    Tyler Eaves <tyler@ml1.net> 
    Very naive and stupid algortihmn.
    It tries taking the modulo of each
    known prime with the test number.
    If modulo = 0 for any number, the
    test is not prime.
    *)
   
let primes = [2];;

(* Modulo tester *)
let fmod x y = x mod y;;

let rec isprime n = (
  let test = List.map (fmod n) primes;
  let testa = Array.of_list test;
  let testa = Array.sort compare testa;
  if testa.(0) = 0 then
    (* Not prime *)
    Printf.printf "%d is not prime.\n" n
    else 
    begin
    Printf.printf "%d IS PRIME!!\n" n;
    let primes = n :: primes 
    end
   );
  isprime n+1
  ;;

isprime 3;;

[-- Attachment #3: Type: text/plain, Size: 100 bytes --]

re out why...), but I think I've figured some of this stuff out, kinda. 
I'm encouraged at least :)

^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23  7:23       ` Tyler Eaves
@ 2003-12-23  8:26         ` Dustin Sallings
  0 siblings, 0 replies; 37+ messages in thread
From: Dustin Sallings @ 2003-12-23  8:26 UTC (permalink / raw)
  To: Tyler Eaves; +Cc: caml-list


On Dec 22, 2003, at 23:23, Tyler Eaves wrote:

> This makes since in general. I'm guess h and t correspond to the head 
> and tail of the lift. It'll probably make more sense tomorrow. It's 
> 2:11 here, and I'd planned to be asleep two hours ago. The replies 
> have kept me up ;)
>
> Based on what I've learned, I've tried to write a simple prime number 
> finder. It doesn't actually *work* or anything (Syntax Error on line 
> 23, can't figure out why...), but I think I've figured some of this 
> stuff out, kinda. I'm encouraged at least.

	I responded to this off-list.  Basically it's learning to program 
without side-effects.  I provided a few side-effect-free 
implementations which can be seen here:

	http://bleu.west.spy.net/~dustin/tmp/primes.ml.html

	I rather like this, as well:  
http://www.bagley.org/~doug/ocaml/Notes/okoans.shtml

--
SPY                      My girlfriend asked me which one I like better.
pub  1024/3CAE01D5 1994/11/03 Dustin Sallings <dustin@spy.net>
|    Key fingerprint =  87 02 57 08 02 D0 DA D6  C8 0F 3E 65 51 98 D8 BE
L_______________________ I hope the answer won't upset her. ____________

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23  4:03 [Caml-list] Frustrated Beginner Tyler Eaves
                   ` (2 preceding siblings ...)
  2003-12-23  6:20 ` Tom Murray
@ 2003-12-23  8:52 ` Stefano Zacchiroli
  2003-12-23 16:47   ` [Caml-list] Ocaml syntax David Brown
  2003-12-23 10:26 ` [Caml-list] Frustrated Beginner Samuel Lacas
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 37+ messages in thread
From: Stefano Zacchiroli @ 2003-12-23  8:52 UTC (permalink / raw)
  To: caml-list

On Mon, Dec 22, 2003 at 11:03:07PM -0500, Tyler Eaves wrote:
> comes to Syntax Errors. It would be so helpful if it could give an error
> message that actually told the programmer what it expected. I realise

Try to compile using camlp4 (e.g. "ocamlc -pp camlp4o" instead of
"ocamlc" alone). It really helps since it print out what the parser is
expecting instead of simply "syntax error".

I also suggest to have a look at the revised syntax described in the
camlp4 manual. I'm not using it because I've discovered it too late and
I've to work everyday with code written in the standard one, but I found
it clearer in a lot of cases. Since you're starting from scratch I think
you can give it a chance.

Hope this helps.
Cheers.

-- 
^Stefano Zacchiroli -- Master in Computer Science @ Uni. Bologna, Italy$
^zack@{cs.unibo.it,debian.org,bononia.it} -- http://www.bononia.it/zack$
^Frequentando il mio maestro mi ero reso conto [.] che la logica poteva$
^servire a molto a condizione di entrarci dentro e poi di uscirne -Adso$

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23  4:03 [Caml-list] Frustrated Beginner Tyler Eaves
                   ` (3 preceding siblings ...)
  2003-12-23  8:52 ` Stefano Zacchiroli
@ 2003-12-23 10:26 ` Samuel Lacas
  2003-12-23 11:01   ` Dustin Sallings
  2003-12-23 16:11 ` Brian Hurt
  2003-12-24  9:49 ` skaller
  6 siblings, 1 reply; 37+ messages in thread
From: Samuel Lacas @ 2003-12-23 10:26 UTC (permalink / raw)
  To: Tyler Eaves; +Cc: caml-list

Tyler Eaves wrote:

> What I'd really like is a site with examples of actual programs. Most of
> the example code I've seen is A: Not commened and B: Recursive
> Mathematical functions. I'd really love to see an example, of say,
> reading in a file, looking at each character, and doing something when
> it encounters, say, a tab. Or any other such program that actually does
> something. 

May be

http://pleac.sourceforge.net/pleac_ocaml.html

may help you here.

sL

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23 10:26 ` [Caml-list] Frustrated Beginner Samuel Lacas
@ 2003-12-23 11:01   ` Dustin Sallings
  2003-12-23 14:34     ` Oleg Trott
  0 siblings, 1 reply; 37+ messages in thread
From: Dustin Sallings @ 2003-12-23 11:01 UTC (permalink / raw)
  To: Samuel Lacas; +Cc: Tyler Eaves, caml-list


On Dec 23, 2003, at 2:26, Samuel Lacas wrote:

> May be
>
> http://pleac.sourceforge.net/pleac_ocaml.html
>
> may help you here.

	Interesting, this has the same problem I had when I tried to create 
fold_lines:

let rec fold_lines f init chan =
   try
     let line = input_line chan in
     fold_lines f (f init line) chan
   with End_of_file -> init

	That ends up not being tail recursive.  I suppose I should get on the 
list and try to make some suggestions.

--
SPY                      My girlfriend asked me which one I like better.
pub  1024/3CAE01D5 1994/11/03 Dustin Sallings <dustin@spy.net>
|    Key fingerprint =  87 02 57 08 02 D0 DA D6  C8 0F 3E 65 51 98 D8 BE
L_______________________ I hope the answer won't upset her. ____________

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23  6:31       ` Michael Jeffrey Tucker
@ 2003-12-23 12:16         ` Richard Jones
  2003-12-23 20:23           ` Dustin Sallings
  0 siblings, 1 reply; 37+ messages in thread
From: Richard Jones @ 2003-12-23 12:16 UTC (permalink / raw)
  Cc: Tyler Eaves, caml-list

On Tue, Dec 23, 2003 at 01:31:42AM -0500, Michael Jeffrey Tucker wrote:
> struct foo_type {
> 	enum {Some, None} opt;
> 	value_t val;
> };
> 
> If the opt field of the structure is set to Some, then you are free to
> look at the val field. If it's none, then the value in the val field is
> unspecified and should be ignored (of course this is more stringently
> enforced in OCaml). In OCaml an "option type" variable has either the
> value "Some xxx" or "None" where xxx is a value. So, a "string option"
> variable might have the value Some "hi", Some "qwery" or None. If you
> pattern match on a variable of this type, as in:

I like to think of the 'string option' type as being a bit like
two concepts in more familiar languages:

(1) NULL pointers in C, or
(2) Scalars in Perl, which can be 'undef'.

For example:

	C			OCaml			Perl

	char *str = "hello";	Some "hello"		$s = "hello"
	char *str = NULL;	None			$s = undef

The basic 'string' type in OCaml can never have a NULL value.  Sure,
it can be an empty string ("") but this is different from it having no
value at all.

BTW, 'string option' is very easily defined in OCaml as:

	type 'a option = None | Some of 'a

which naturally brings us to the subject of polymorphic types.  The above
definition defines a whole family of types, which can be thought of like
a wildcard '* option' where '*' is any existing type.  eg:

	Expression	Type

	Some 1		int option
	Some 'c'	char option
	Some [1]	int list option
	Some (Some 1)	int option option
	Some []		'a list option		(tricky one, this)

etc.

Rich.

-- 
Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj
Merjis Ltd. http://www.merjis.com/ - improving website return on investment
MOD_CAML lets you run type-safe Objective CAML programs inside the Apache
webserver. http://www.merjis.com/developers/mod_caml/

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23 11:01   ` Dustin Sallings
@ 2003-12-23 14:34     ` Oleg Trott
  2003-12-23 20:25       ` Dustin Sallings
  0 siblings, 1 reply; 37+ messages in thread
From: Oleg Trott @ 2003-12-23 14:34 UTC (permalink / raw)
  To: Dustin Sallings, Samuel Lacas; +Cc: Tyler Eaves, caml-list

On Tuesday 23 December 2003 06:01 am, Dustin Sallings wrote:
> On Dec 23, 2003, at 2:26, Samuel Lacas wrote:
> > May be
> >
> > http://pleac.sourceforge.net/pleac_ocaml.html
> >
> > may help you here.
>
> 	Interesting, this has the same problem I had when I tried to create
> fold_lines:
>
> let rec fold_lines f init chan =
>    try
>      let line = input_line chan in
>      fold_lines f (f init line) chan
>    with End_of_file -> init
>
> 	That ends up not being tail recursive.  I suppose I should get on the
> list and try to make some suggestions.

Non-recursive version

let fold_lines f init chan =
   let res = ref init in
   (try while true do
      res  := f !res (input_line chan)
   done with End_of_file -> ());
   !res

Tail-recursive version:

let rec fold_lines f init chan =
   match try Some (input_line chan) with End_of_file -> None with
   | Some line -> fold_lines f (f init line) chan
   | None -> init

(But I don't think "fold" functions are useful for a beginner, especially one 
who's still having problems with the syntax aspect of the language. TE, look 
elsewhere)

-- 
Oleg Trott <oleg_trott@columbia.edu>

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23  4:03 [Caml-list] Frustrated Beginner Tyler Eaves
                   ` (4 preceding siblings ...)
  2003-12-23 10:26 ` [Caml-list] Frustrated Beginner Samuel Lacas
@ 2003-12-23 16:11 ` Brian Hurt
  2003-12-23 16:20   ` Sven Luther
  2003-12-24  9:49 ` skaller
  6 siblings, 1 reply; 37+ messages in thread
From: Brian Hurt @ 2003-12-23 16:11 UTC (permalink / raw)
  To: Tyler Eaves; +Cc: caml-list


I notice a lot of replies to this already, I'll add mine.

On Mon, 22 Dec 2003, Tyler Eaves wrote:

> First a bit of background:
> 
> I'm a 19 yr old computer science student, been programming for perhaps
> 10 years. Started (Like so many did in the early '90s with QBasic in
> DOS. These days most of my work is done in either PHP (web stuff) or
> Python (everything else) with the occasional bit of C for stuff that
> needs to run really fast. I'm also passingly familiar with a number of
> other langauges (C++, Java, Perl, etc). 
> 
> So why is O'Caml giving me so much trouble? I've been trying to pick it
> up for about a week now, read various online tutorials. I'm just having
> no luck at all. My largest program to date (that works) is all of 3
> lines long, and simply printed out the command line arguments passed to
> the program.

All programming languages are not the same.  There programming 
"paradigms"- literally different ways of thinking about programming.  
Imperitive/Procedural (C, most Perl, etc) is one way of thinking about 
things.  Object Oriented (Python, Java, C++, etc) is another.  Functional 
(Lisp, Scheme, Ocaml) is a third.  Picking up a new language in a paradigm 
you are already familiar with is easy- if you know C++ and Java, picking 
up Python or Ruby is a week or three at most, no problem.  Picking up a 
language in a different paradigm is much harder- not only do you have to 
pick up a new syntax, you have to pick up a new way of thinking about 
programming.

It's worth the effort, IMHO.  Some problems are easier to implement in 
some paradigms than other (take a look at Motif programming to get a 
deeper appreciation of OO for GUI libraries).  Perl, Python, and (IIRC) 
Ruby all give you some ability to write functional code.  Things you learn 
in Ocaml are applicable elsewhere (not to mention the fact that you get to 
use Ocaml).

To use an analogy, if you know English, learning German or French (while a 
little hard) isn't that difficult, but the cultures share a common 
concept base.  You have to learn a new syntax and a new vocabulary, but 
very few new concepts.  Learning Chinese or Navaho is a lot more 
difficult, because not only do you have the problems of learning German or 
French, but you also have to learn concepts, new ways of thinking.

> 
> My biggest source of problems seems to be the syntax. I'm totally
> confused as far as ; vs ;; vs nothing, when to use ( ), and things of
> the like. 

This took some rethinking on my part as well.  The trick is to remember
that ';' is an operator, like '+' and '.' are.  Actually, the best
comparison is with the comma operator in C.  If the type of '+' is int ->
int -> int ('+' takes two int parameters and returns an int), then the
type of the ';' operator is unit -> 'a -> 'a (or more correctly, 'a -> 'b
-> 'b).  One you start thinking of ';' as an operator and not a statement
seperator, it becomes more obvious that an ocaml function body is simply a
single expression, and not a series of statements.  ';;' is more close to
what ';' in most languages mean- it's what ends an expression.

When do you use parenthesis?  The same times you'd use them in any other 
language- to disambiguate expressions.  Note that begin/end are 
replacements for parens, and occassionally look nicer.  

> It doesn't help that the compiler is completly naive when it
> comes to Syntax Errors. It would be so helpful if it could give an error
> message that actually told the programmer what it expected. I realise
> that O'Camls syntax allows many things, so that it may be hard to say
> exactly what it WAS expecting in all cases, but surely SOMETHING
> meaningful, besides a charater postion could be given? 

Heh.  I've got to agree that Ocaml's error messages leave something to be 
desired.  But, speaking with 15 years more programming experience, there 
will come a time when you will give your left arm to be magically told 
just the line number of the bug.

Simple experience with the language helps here.  I find myself more easily 
being able to diagnose what Ocaml is really complaining about the more I 
use a language.  Of course, this is true of any language.

> 
> What I'd really like is a site with examples of actual programs. Most of
> the example code I've seen is A: Not commened and B: Recursive
> Mathematical functions. I'd really love to see an example, of say,
> reading in a file, looking at each character, and doing something when
> it encounters, say, a tab. Or any other such program that actually does
> something. 

Except how you write a program to do exactly that in Ocaml is to write a 
recursive mathematical function:

let count_tabs ins =
	let rec loop cnt =
		let eof, c = 
			try
				false, (input_char ins)
			with
				| End_of_file -> true, ' '
		in
		if eof then
			cnt
		else if (c = '\t') then
			loop (cnt + 1)
		else
			loop cnt
	in
	loop 0
;;

Learn recursion.  It's an incredibly powerful concept, and it's usefull in 
almost every language.  Including mathematics.

> 
> I really want to like O'Caml. It seems to offer a very nice feature set,
> and the ability to compile to super-fast native code is the icing on the
> cake. Right now though, I'm frustrated and on the verge of giving up.
> 

Don't give up.  You're learning more than just a new programming language.

-- 
"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
                                - Gene Spafford 
Brian

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23 16:11 ` Brian Hurt
@ 2003-12-23 16:20   ` Sven Luther
  2003-12-23 16:52     ` David Brown
  2003-12-23 17:39     ` Brian Hurt
  0 siblings, 2 replies; 37+ messages in thread
From: Sven Luther @ 2003-12-23 16:20 UTC (permalink / raw)
  To: Brian Hurt; +Cc: Tyler Eaves, caml-list

On Tue, Dec 23, 2003 at 11:11:19AM -0500, Brian Hurt wrote:
> > My biggest source of problems seems to be the syntax. I'm totally
> > confused as far as ; vs ;; vs nothing, when to use ( ), and things of
> > the like. 
> 
> This took some rethinking on my part as well.  The trick is to remember
> that ';' is an operator, like '+' and '.' are.  Actually, the best
> comparison is with the comma operator in C.  If the type of '+' is int ->
> int -> int ('+' takes two int parameters and returns an int), then the
> type of the ';' operator is unit -> 'a -> 'a (or more correctly, 'a -> 'b
> -> 'b).  One you start thinking of ';' as an operator and not a statement
> seperator, it becomes more obvious that an ocaml function body is simply a
> single expression, and not a series of statements.  ';;' is more close to
> what ';' in most languages mean- it's what ends an expression.

Mmm, not really convinced. For me ;, is a shortcut for :

  let () = ... in

Actually, i believe that both constructs result in the exact same code.

That said, i guess it would not really help someone coming from the
imperative world, and not used to the let ... = ... in construct.

Friendly,

Sven Luther

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* [Caml-list] Ocaml syntax.
  2003-12-23  8:52 ` Stefano Zacchiroli
@ 2003-12-23 16:47   ` David Brown
  2003-12-23 20:19     ` Dustin Sallings
                       ` (2 more replies)
  0 siblings, 3 replies; 37+ messages in thread
From: David Brown @ 2003-12-23 16:47 UTC (permalink / raw)
  To: caml-list

On Tue, Dec 23, 2003 at 09:52:59AM +0100, Stefano Zacchiroli wrote:

> I also suggest to have a look at the revised syntax described in the
> camlp4 manual. I'm not using it because I've discovered it too late and
> I've to work everyday with code written in the standard one, but I found
> it clearer in a lot of cases. Since you're starting from scratch I think
> you can give it a chance.

The revised syntax to ocaml attempts to provide an cleaner, more
consistent syntax for the language, and it does a fairly good job of it.
However, I don't see very much code written in the revised syntax.  I've
thought about why, and come up with an interesting theory.

The native ocaml syntax has a bunch of strange inconsistencies.  Human
languages also tend to have lots of inconsistencies, especially around
the core words.  Perhaps there is something about our brains that works
best when the core aspect of a language is inconsistent.

After a bout of frustration with the native ocaml syntax, I know
consider it to be the most pleasing syntax of any language I've ever
used.  It is terse, yet readable.  There is also something about it,
now, that feels very natural to me.

My suggestion to those frustrated by the syntax: persist.  There are
some difficulties, but there are also some significant benefits to it as
well.

Dave Brown

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23 16:20   ` Sven Luther
@ 2003-12-23 16:52     ` David Brown
  2003-12-23 20:32       ` Dustin Sallings
  2003-12-23 17:39     ` Brian Hurt
  1 sibling, 1 reply; 37+ messages in thread
From: David Brown @ 2003-12-23 16:52 UTC (permalink / raw)
  To: Sven Luther; +Cc: Brian Hurt, Tyler Eaves, caml-list

On Tue, Dec 23, 2003 at 05:20:04PM +0100, Sven Luther wrote:

> Mmm, not really convinced. For me ;, is a shortcut for :
> 
>   let () = ... in
> 
> Actually, i believe that both constructs result in the exact same code.

Plus there are some places where let () = ... in makes things easier,
since it allows multiple expressions after a then or else without
needing a begin/end wrapping.

Dave

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23 16:20   ` Sven Luther
  2003-12-23 16:52     ` David Brown
@ 2003-12-23 17:39     ` Brian Hurt
  2003-12-24  9:35       ` Jacques Garrigue
  1 sibling, 1 reply; 37+ messages in thread
From: Brian Hurt @ 2003-12-23 17:39 UTC (permalink / raw)
  To: Sven Luther; +Cc: Tyler Eaves, caml-list

On Tue, 23 Dec 2003, Sven Luther wrote:

> On Tue, Dec 23, 2003 at 11:11:19AM -0500, Brian Hurt wrote:
> > > My biggest source of problems seems to be the syntax. I'm totally
> > > confused as far as ; vs ;; vs nothing, when to use ( ), and things of
> > > the like. 
> > 
> > This took some rethinking on my part as well.  The trick is to remember
> > that ';' is an operator, like '+' and '.' are.  Actually, the best
> > comparison is with the comma operator in C.  If the type of '+' is int ->
> > int -> int ('+' takes two int parameters and returns an int), then the
> > type of the ';' operator is unit -> 'a -> 'a (or more correctly, 'a -> 'b
> > -> 'b).  One you start thinking of ';' as an operator and not a statement
> > seperator, it becomes more obvious that an ocaml function body is simply a
> > single expression, and not a series of statements.  ';;' is more close to
> > what ';' in most languages mean- it's what ends an expression.
> 
> Mmm, not really convinced. For me ;, is a shortcut for :
> 
>   let () = ... in

Or more accurately
	let _ = ... in ...

> 
> Actually, i believe that both constructs result in the exact same code.
> 
> That said, i guess it would not really help someone coming from the
> imperative world, and not used to the let ... = ... in construct.

Yep.  The change in thinking is that ';' doesn't seperate statements, but
combines two expressions into one expression.  Your understanding is more
technically correct, but I was trying to draw analogies to '+' and ','.

-- 
"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
                                - Gene Spafford 
Brian

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Ocaml syntax.
  2003-12-23 16:47   ` [Caml-list] Ocaml syntax David Brown
@ 2003-12-23 20:19     ` Dustin Sallings
  2003-12-23 21:03       ` Eric Merritt
  2003-12-23 21:52     ` brogoff
  2003-12-30  8:14     ` dmitry grebeniuk
  2 siblings, 1 reply; 37+ messages in thread
From: Dustin Sallings @ 2003-12-23 20:19 UTC (permalink / raw)
  To: David Brown; +Cc: caml-list


On Dec 23, 2003, at 8:47, David Brown wrote:

> The revised syntax to ocaml attempts to provide an cleaner, more
> consistent syntax for the language, and it does a fairly good job of 
> it.
> However, I don't see very much code written in the revised syntax.  
> I've
> thought about why, and come up with an interesting theory.

	Actually, for me, it's that I've been spending time learning the base 
ocaml stuff and I get this strange feeling that camlp4 is an 
unnecessary extra chunk of language I'd need to learn.  From what I've 
been reading, it seems like I should probably get over that.  It's 
certainly not because I favor inconsistencies.

	Like with most things, I'll probably eventually start playing with it 
and wonder why I was so stupid to deny myself this great enhancement.

--
SPY                      My girlfriend asked me which one I like better.
pub  1024/3CAE01D5 1994/11/03 Dustin Sallings <dustin@spy.net>
|    Key fingerprint =  87 02 57 08 02 D0 DA D6  C8 0F 3E 65 51 98 D8 BE
L_______________________ I hope the answer won't upset her. ____________

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23 12:16         ` Richard Jones
@ 2003-12-23 20:23           ` Dustin Sallings
  0 siblings, 0 replies; 37+ messages in thread
From: Dustin Sallings @ 2003-12-23 20:23 UTC (permalink / raw)
  To: Richard Jones; +Cc: Tyler Eaves, caml-list


On Dec 23, 2003, at 4:16, Richard Jones wrote:

> I like to think of the 'string option' type as being a bit like
> two concepts in more familiar languages:
>
> (1) NULL pointers in C, or
> (2) Scalars in Perl, which can be 'undef'.

	I think of it more like SQL.  Any given variable may be defined to be 
nullable or not, but a variable that is not nullable an never be null.  
The main difference is that the compiler enforces that you check for 
null on nullable fields.

--
SPY                      My girlfriend asked me which one I like better.
pub  1024/3CAE01D5 1994/11/03 Dustin Sallings <dustin@spy.net>
|    Key fingerprint =  87 02 57 08 02 D0 DA D6  C8 0F 3E 65 51 98 D8 BE
L_______________________ I hope the answer won't upset her. ____________

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23 14:34     ` Oleg Trott
@ 2003-12-23 20:25       ` Dustin Sallings
  0 siblings, 0 replies; 37+ messages in thread
From: Dustin Sallings @ 2003-12-23 20:25 UTC (permalink / raw)
  To: Oleg Trott; +Cc: Samuel Lacas, Tyler Eaves, caml-list


On Dec 23, 2003, at 6:34, Oleg Trott wrote:

> (But I don't think "fold" functions are useful for a beginner, 
> especially one
> who's still having problems with the syntax aspect of the language. 
> TE, look
> elsewhere)

	My understanding is that pleac is a generic cookbook, which means that 
it should provide recipes that generally work.  The first time I needed 
to fold a file I used an implementation exactly like the one at pleac 
and it failed.

	I'm asking on the list, though.

--
SPY                      My girlfriend asked me which one I like better.
pub  1024/3CAE01D5 1994/11/03 Dustin Sallings <dustin@spy.net>
|    Key fingerprint =  87 02 57 08 02 D0 DA D6  C8 0F 3E 65 51 98 D8 BE
L_______________________ I hope the answer won't upset her. ____________

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23 16:52     ` David Brown
@ 2003-12-23 20:32       ` Dustin Sallings
  2003-12-24 10:41         ` Issac Trotts
  0 siblings, 1 reply; 37+ messages in thread
From: Dustin Sallings @ 2003-12-23 20:32 UTC (permalink / raw)
  To: David Brown; +Cc: Brian Hurt, Tyler Eaves, caml-list, Sven Luther


On Dec 23, 2003, at 8:52, David Brown wrote:

> Plus there are some places where let () = ... in makes things easier,
> since it allows multiple expressions after a then or else without
> needing a begin/end wrapping.

	I've been getting in the habit of doing this kind of thing:

	if (expression) then (
		stuff
	) else (
		other stuff
	)

	It gives me % matching in vim, allows me to insert debug statements 
when I'm screwing up, and looks fairly clean and unambiguous.

--
SPY                      My girlfriend asked me which one I like better.
pub  1024/3CAE01D5 1994/11/03 Dustin Sallings <dustin@spy.net>
|    Key fingerprint =  87 02 57 08 02 D0 DA D6  C8 0F 3E 65 51 98 D8 BE
L_______________________ I hope the answer won't upset her. ____________

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Ocaml syntax.
  2003-12-23 20:19     ` Dustin Sallings
@ 2003-12-23 21:03       ` Eric Merritt
  0 siblings, 0 replies; 37+ messages in thread
From: Eric Merritt @ 2003-12-23 21:03 UTC (permalink / raw)
  To: caml-list

> 	Actually, for me, it's that I've been spending time
> learning the base 
> ocaml stuff and I get this strange feeling that
> camlp4 is an 
> unnecessary extra chunk of language I'd need to
> learn.  From what I've 
> been reading, it seems like I should probably get
> over that.  It's 
> certainly not because I favor inconsistencies.

 So far I have stayed away from the camlp4 revised
syntax, its just not as appealing to me. I tend to
think of the standard ocaml syntax as somewhat clean
and the p4 modifications as somewhat kludgey. Thats
just my opinion however.
 
> 	Like with most things, I'll probably eventually
> start playing with it 
> and wonder why I was so stupid to deny myself this
> great enhancement.
> 
 
 I havn't seen any compelling reason to switch. As
long as the core ocaml language isn't going to migrate
to it I don't see the point.

__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Ocaml syntax.
  2003-12-23 16:47   ` [Caml-list] Ocaml syntax David Brown
  2003-12-23 20:19     ` Dustin Sallings
@ 2003-12-23 21:52     ` brogoff
  2003-12-24 10:27       ` skaller
  2003-12-30  8:14     ` dmitry grebeniuk
  2 siblings, 1 reply; 37+ messages in thread
From: brogoff @ 2003-12-23 21:52 UTC (permalink / raw)
  To: caml-list

On Tue, 23 Dec 2003, David Brown wrote:
> On Tue, Dec 23, 2003 at 09:52:59AM +0100, Stefano Zacchiroli wrote:
>
> > I also suggest to have a look at the revised syntax described in the
> > camlp4 manual. I'm not using it because I've discovered it too late and
> > I've to work everyday with code written in the standard one, but I found
> > it clearer in a lot of cases. Since you're starting from scratch I think
> > you can give it a chance.
>
> The revised syntax to ocaml attempts to provide an cleaner, more
> consistent syntax for the language, and it does a fairly good job of it.
> However, I don't see very much code written in the revised syntax.  I've
> thought about why, and come up with an interesting theory.

Gack. Yet another misleading analogy with human languages? I think Wirth was
right when he said we shouldn't call them programming languages.

Stefano is correct, but doesn't go far enough. The reason many of us who like
Revised better don't use it is that we don't work in a vacuum, but we work with
other programmers, and use an existing code base. It's hard enough to get
programmers to learn OCaml, with such a small user community and (comparatively)
such a small amount of teaching material.

Also, it's not clear what's going to happen to Revised now that ddr left.

> The native ocaml syntax has a bunch of strange inconsistencies.  Human
> languages also tend to have lots of inconsistencies, especially around
> the core words.  Perhaps there is something about our brains that works
> best when the core aspect of a language is inconsistent.

If I believed that, I'd be using Perl and C++ more than I do. Nope, I think
"hysterical raisins" and human laziness/inertia/risk-avoidance explains a lot.

-- Brian



-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23 17:39     ` Brian Hurt
@ 2003-12-24  9:35       ` Jacques Garrigue
  0 siblings, 0 replies; 37+ messages in thread
From: Jacques Garrigue @ 2003-12-24  9:35 UTC (permalink / raw)
  To: bhurt; +Cc: caml-list

From: Brian Hurt <bhurt@spnz.org>

> > Mmm, not really convinced. For me ;, is a shortcut for :
> > 
> >   let () = ... in
> 
> Or more accurately
> 	let _ = ... in ...

Well, both wrong :-)
The error/warning behaviour differs.

Actually, there are 4 possibilities:

construct  \  type    unit      'a       function       other
let () = ... in ...   OK        OK(*)    error          error
let _ = ... in ...    OK        OK       OK             OK
ignore (...); ...     OK        OK       warning        OK
...; ...              OK        OK       warning        warning

(*) the result type is unified with unit.

So, you have plenty of choice on how to sequence your code.
Additionally there are compiler options to ignore warnings or turn
them into errors.
(Having been bitten several times, I actually suggest avoiding "let _"
as it discards the very useful partial application warning. But of
course your need it for side-effecting code returning a function.)

     Jacques

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23  4:03 [Caml-list] Frustrated Beginner Tyler Eaves
                   ` (5 preceding siblings ...)
  2003-12-23 16:11 ` Brian Hurt
@ 2003-12-24  9:49 ` skaller
  6 siblings, 0 replies; 37+ messages in thread
From: skaller @ 2003-12-24  9:49 UTC (permalink / raw)
  To: Tyler Eaves; +Cc: caml-list

On Tue, 2003-12-23 at 15:03, Tyler Eaves wrote:

> So why is O'Caml giving me so much trouble? 

Because the syntax is foreign.

> My biggest source of problems seems to be the syntax. I'm totally
> confused as far as ; vs ;; vs nothing, 

Yeah. The 'top level' is syntactically and semantically different
to the inner levels of Ocaml.

(1) At the top level you can write a sequence of definitions
or evaluate a sequence of expressions. This is not possible
anywhere else.

(2) If the definition/expression starts with a keyword
you don't need to use ';;' before it. For example:

	let f x = x
	let g x = x * x
	;;          (* needed because .. *)
	f x * g x   (* no keyword starts this *)
	;;          (* needed to force evaluation of expression NOW *)

At the inner level, you cannot write

	let h x =
		let f x = x
		let g x = x * x
		;;
		f x * g x
	;;

Inner expressions are evaluated 'entirely', not one at a time like
the top level. So you must write this:

	let h x =
		let f x = x in
		let g x = x * x in
		f x * g x
	;; (* end top level definition *)

You use ';' for sequencing evaluations:

	print_endline "Hello";
	print_endline "World"
	;;

This is a *single* expression evaluated, whereas

	print_endline "Hello"
	;;
	print_endline "World"
	;;

is two evaluations. (Try this in the 'ocaml' interpreter).


> when to use ( ), and things of
> the like. It doesn't help that the compiler is completly naive when it
> comes to Syntax Errors. 

The reason is: the parser is a Yacc tool which means it
uses a LR parsing technique. When it can't parse something,
it has no choice than to say 'syntax error'. Its an error,
there's no way to say what the error is.

One way around this in LR parsers is to include error
productions in the grammar... that is, make the parser
parse common errors.

Another way, is to parse something more general than 
permitted first, then check constraints. This is certainly
done with type checking for example.


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Ocaml syntax.
  2003-12-23 21:52     ` brogoff
@ 2003-12-24 10:27       ` skaller
  2003-12-24 11:42         ` Peter Jolly
  0 siblings, 1 reply; 37+ messages in thread
From: skaller @ 2003-12-24 10:27 UTC (permalink / raw)
  To: brogoff; +Cc: caml-list

On Wed, 2003-12-24 at 08:52, brogoff@speakeasy.net wrote:

> > The native ocaml syntax has a bunch of strange inconsistencies. 

> If I believed that, I'd be using Perl and C++ more than I do. Nope, I think
> "hysterical raisins" and human laziness/inertia/risk-avoidance explains a lot.

Some of the 'inconsistencies' aren't: an 'arbitrary' decision
was made, and the result seems 'inconsistent' only when
you're not the beneficiary of the choice made :-)

As a comparison, Felix mimics many Ocaml constructions
but uses slightly different syntax in some cases.
In particular:

	<statement> ;
	match .. with .. endmatch
	if .. then .. else .. endif

but I still use

	let .. in .. 

unterminated. My opinion is that the endmatch
is ugly, longwinded, but otherwise desirable.

However, the if/then/else/endif leads to a
lot of extra ugly 'endif' keywords for chains,
which also force incorrect nesting.

The fix of course is to include 

	elif

as a suboption. I'm seriously thinking
of the equivalent for matches:

	match .. with
	...
	elmatch .. with
	..
	else 
	..
	endmatch

and now, the original Ocaml syntax looks simpler .. 

especially when you consider that 'elif' makes
sense in a match, and 'elmatch' makes sense in
a conditional ... 


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Frustrated Beginner
  2003-12-23 20:32       ` Dustin Sallings
@ 2003-12-24 10:41         ` Issac Trotts
  0 siblings, 0 replies; 37+ messages in thread
From: Issac Trotts @ 2003-12-24 10:41 UTC (permalink / raw)
  To: caml-list

On Tuesday 23 December 2003 12:32, Dustin Sallings wrote:
> On Dec 23, 2003, at 8:52, David Brown wrote:
> > Plus there are some places where let () = ... in makes things easier,
> > since it allows multiple expressions after a then or else without
> > needing a begin/end wrapping.
>
> 	I've been getting in the habit of doing this kind of thing:
>
> 	if (expression) then (
> 		stuff
> 	) else (
> 		other stuff
> 	)
>
> 	It gives me % matching in vim, allows me to insert debug statements
> when I'm screwing up, and looks fairly clean and unambiguous.

You can match begin/end and other things if you make a file called
$HOME/.vim/ftplugin/ocaml.vim with these lines:

let b:mw='\<let\>:\<and\>:\(\<in\>\|;;\),'
let b:mw=b:mw . '\<if\>:\<then\>:\<else\>,\<do\>:\<done\>,'
let b:mw=b:mw . '\<\(object\|sig\|struct\|begin\)\>:\<end\>'
let b:match_words=mw

Issac

>
> --
> SPY                      My girlfriend asked me which one I like better.
> pub  1024/3CAE01D5 1994/11/03 Dustin Sallings <dustin@spy.net>
>
> |    Key fingerprint =  87 02 57 08 02 D0 DA D6  C8 0F 3E 65 51 98 D8 BE
>
> L_______________________ I hope the answer won't upset her. ____________
>
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives:
> http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ:
> http://caml.inria.fr/FAQ/ Beginner's list:
> http://groups.yahoo.com/group/ocaml_beginners

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Ocaml syntax.
  2003-12-24 10:27       ` skaller
@ 2003-12-24 11:42         ` Peter Jolly
  2003-12-24 12:19           ` skaller
  0 siblings, 1 reply; 37+ messages in thread
From: Peter Jolly @ 2003-12-24 11:42 UTC (permalink / raw)
  To: caml-list

skaller wrote:
> 	match .. with .. endmatch
...
> My opinion is that the endmatch
> is ugly, longwinded, but otherwise desirable.

How is this different from Caml's idiom, "begin match .. with .. end"?

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Ocaml syntax.
  2003-12-24 11:42         ` Peter Jolly
@ 2003-12-24 12:19           ` skaller
  0 siblings, 0 replies; 37+ messages in thread
From: skaller @ 2003-12-24 12:19 UTC (permalink / raw)
  To: caml-list

On Wed, 2003-12-24 at 22:42, Peter Jolly wrote:
> skaller wrote:
> > 	match .. with .. endmatch
> ...
> > My opinion is that the endmatch
> > is ugly, longwinded, but otherwise desirable.
> 
> How is this different from Caml's idiom, "begin match .. with .. end"?

It's enforced by the parser.


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Ocaml syntax.
  2003-12-23 16:47   ` [Caml-list] Ocaml syntax David Brown
  2003-12-23 20:19     ` Dustin Sallings
  2003-12-23 21:52     ` brogoff
@ 2003-12-30  8:14     ` dmitry grebeniuk
  2003-12-30 17:48       ` David Brown
  2 siblings, 1 reply; 37+ messages in thread
From: dmitry grebeniuk @ 2003-12-30  8:14 UTC (permalink / raw)
  To: David Brown; +Cc: caml-list

Hello, David.

DB> The revised syntax to ocaml attempts to provide an cleaner, more
DB> consistent syntax for the language, and it does a fairly good job of it.
DB> However, I don't see very much code written in the revised syntax.  I've
DB> thought about why, and come up with an interesting theory.

DB> The native ocaml syntax has a bunch of strange inconsistencies.  Human
DB> languages also tend to have lots of inconsistencies, especially around
DB> the core words.  Perhaps there is something about our brains that works
DB> best when the core aspect of a language is inconsistent.

  Quite interesting, and maybe it is true, but I have more simple theory.
  First time I knew about revised syntax only when started to write
camlp4-extensions, and at that time I've used caml about one year.  No
any note, nor even small remark about revised syntax was seen before
reading camlp4 documentation.  Larry said right about impatience, lazyness
and hubris:  most of us are too lazy to re-learn syntax of programming
language after using it enough time.

-- 
wbr,
 dmitry                          mailto:gds-mlsts@moldavcable.com

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

* Re: [Caml-list] Ocaml syntax.
  2003-12-30  8:14     ` dmitry grebeniuk
@ 2003-12-30 17:48       ` David Brown
  0 siblings, 0 replies; 37+ messages in thread
From: David Brown @ 2003-12-30 17:48 UTC (permalink / raw)
  To: dmitry grebeniuk; +Cc: caml-list

On Tue, Dec 30, 2003 at 10:14:26AM +0200, dmitry grebeniuk wrote:

>   Quite interesting, and maybe it is true, but I have more simple theory.
>   First time I knew about revised syntax only when started to write
> camlp4-extensions, and at that time I've used caml about one year.  No
> any note, nor even small remark about revised syntax was seen before
> reading camlp4 documentation.  Larry said right about impatience, lazyness
> and hubris:  most of us are too lazy to re-learn syntax of programming
> language after using it enough time.

In my brief time trying to use the revised syntax, I found it to be more
cumbersome.  The native syntax has a very terse feel to it.  I think the
inconsistencies actually help make it terse.

Dave

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 37+ messages in thread

end of thread, other threads:[~2003-12-30 17:48 UTC | newest]

Thread overview: 37+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-12-23  4:03 [Caml-list] Frustrated Beginner Tyler Eaves
2003-12-23  4:19 ` jayanta nath
2003-12-23  5:34   ` Matt Gushee
2003-12-23  6:11     ` Tyler Eaves
2003-12-23  6:21       ` Michael Vanier
2003-12-23  6:31       ` Michael Jeffrey Tucker
2003-12-23 12:16         ` Richard Jones
2003-12-23 20:23           ` Dustin Sallings
2003-12-23  6:32       ` Shawn Wagner
2003-12-23  6:43       ` Matt Gushee
2003-12-23  5:58 ` Dustin Sallings
     [not found]   ` <EAEE2FF2-3510-11D8-B3A1-000A9584A16E@ml1.net>
2003-12-23  6:53     ` Dustin Sallings
2003-12-23  7:23       ` Tyler Eaves
2003-12-23  8:26         ` Dustin Sallings
2003-12-23  6:20 ` Tom Murray
2003-12-23  8:52 ` Stefano Zacchiroli
2003-12-23 16:47   ` [Caml-list] Ocaml syntax David Brown
2003-12-23 20:19     ` Dustin Sallings
2003-12-23 21:03       ` Eric Merritt
2003-12-23 21:52     ` brogoff
2003-12-24 10:27       ` skaller
2003-12-24 11:42         ` Peter Jolly
2003-12-24 12:19           ` skaller
2003-12-30  8:14     ` dmitry grebeniuk
2003-12-30 17:48       ` David Brown
2003-12-23 10:26 ` [Caml-list] Frustrated Beginner Samuel Lacas
2003-12-23 11:01   ` Dustin Sallings
2003-12-23 14:34     ` Oleg Trott
2003-12-23 20:25       ` Dustin Sallings
2003-12-23 16:11 ` Brian Hurt
2003-12-23 16:20   ` Sven Luther
2003-12-23 16:52     ` David Brown
2003-12-23 20:32       ` Dustin Sallings
2003-12-24 10:41         ` Issac Trotts
2003-12-23 17:39     ` Brian Hurt
2003-12-24  9:35       ` Jacques Garrigue
2003-12-24  9:49 ` skaller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).