caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Dustin Sallings <dustin@spy.net>
To: Tyler Eaves <tyler@ml1.net>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Frustrated Beginner
Date: Mon, 22 Dec 2003 21:58:38 -0800	[thread overview]
Message-ID: <0D3B5ECF-350D-11D8-A8F7-000393CB0F1E@spy.net> (raw)
In-Reply-To: <1072152186.59938.30.camel@tylere>


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


  parent reply	other threads:[~2003-12-23  5:58 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-12-23  4:03 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 [this message]
     [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

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=0D3B5ECF-350D-11D8-A8F7-000393CB0F1E@spy.net \
    --to=dustin@spy.net \
    --cc=caml-list@inria.fr \
    --cc=tyler@ml1.net \
    /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;
as well as URLs for NNTP newsgroup(s).