Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] OCaml popularity
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Max Kirillov <max630@m...>
Subject: Re: [Caml-list] OCaml popularity (long!)
On Tue, Mar 11, 2003 at 12:47:56PM -0700, mgushee@havenrock.com wrote:
> My other big problem with Haskell was ... you guessed it: monads! I 
> must have read every introduction to monads that's available on line 
> (and there are at least half a dozen), but I still don't really 
> understand them. Without monads, you can't do any real work in 
> Haskell, and monads are universally acknowledged to be a difficult 
> concept. Yet every introductory text I have seen on Haskell insists 
> that you learn the theory of monads before you can learn how to do 
> things like I/O.
> 
> Since I don't understand monads, maybe I'm missing something, but 
> this insistence on theory before practice seems like pure dogma to 
> me. I've even seen two or three Haskell tutorials that open with 
> statements to the effect that "I could teach you how to write a 
> Hello, World program, but I won't, because then you wouldn't 
> understand why Haskell is so great."
> 
> I don't buy that argument--at least I don't accept that it's 
> universally true. People have different learning styles, but many 
> learn best by doing. I don't know how typical I am, but I find that a 
> good experience working with a new language naturally leads to a 
> desire for deeper understanding.

I wonder, why people are so troubled by monads. I suppose the answer is:
bacause they try to "understand" it. But there are really nothing to
understand! You don't need to have PhD in math or read Leibnitz to use
the IO monad, and the IO monad is the only monad a beginner could
need (list/maybe monads looks like just incidental thing to me; ST s
monads are very similar to IO monads, and can, for a beginners, be
replaced by it; the only I can recall now is monad of parsers, but,
consider, it is not an introductory question at all).

This is very simple:

Values in haskell are not a "contents of variables" or omething like
this; they are values in concept. Function (that is to produce them),
can therefore be called just anytime compiler will decide it to do. They
can be executed multiple time (e.g. when inlined) or not executed at
all, when we don't need the value. Beacause of that, Haskell functions
can not represent actions.

Instead, that does values of type IO a.  A notion "IO a" means "make
something, and provide a [way to compute] value of type a". Note here:
not "a -> IO b", but "IO a" -- action without an argument is quite
meaningful thing.  For example, (counter :: IO Integer) produces
infinitely increasing numbers. (time :: IO Double) returns time in some
units, or (is_connected :: IO Bool) reports, is, for example, our
program connected to the server and so on. Gong back to ocaml, I would
say that "IO a" is roughly equivalent to "unit -> 'a" (indeed, "(unit ->
'a Lazy.t) Lazy.t" would be more right). Now remember, have you ever had
to add the "unit" argument to your ocaml functions (in fact, not very
often -- since ocaml is strict, there's always a possibility to use some
of real argument to trigger the execution).

To use actions, there must be a way to combine them. There are operators
">>" and ">>=". The first combines two actions in an action, which is a
sequence of them. The second form is intended to propagate the results
of execution to the succeeding actions. It combnes an action of type IO
a and function taking a value of type a and producing an action, which,
when executed, first executes the first action, then takes it result,
computes the second action and executes it.

"do" notation inteded to make the sequences of action look similar to
the imperative languages.

Since a program is definetly an action, your main function must have
type "IO something". It really does: "IO ()" -- takes nothing, doing
something, produces nothing.

That's just all. Is it so hard?

> --
> Matt Gushee
> Englewood, CO USA

-- 
Max

-------------------
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