[Haskell-cafe] Re: Haskell vs OCaml

Michael Vanier mvanier at cs.caltech.edu
Tue May 3 18:41:22 EDT 2005


Marcin gives a good capsule description of the differences between ocaml
and haskell.  Let me add my two cents.

I also learned ocaml before learning haskell, and the biggest single
difference I found is that haskell is a lazy, purely functional language
and ocaml is a strict, "mostly functional" language.  So all the IO monad
stuff in haskell doesn't exist in ocaml -- writing imperative code in ocaml
is much like writing imperative code in C, albeit with different syntax.
This makes it much easier to transition to for C or python programmers, but
it's easy for side-effects to work their way into your program without you
realizing it, which means you lose a lot of the advantages of functional
programming -- unless you take a lot of care to make sure that most of your
functions are in fact purely functional (and the compiler won't help you
with that).

Another big difference between ocaml and haskell is that haskell has type
classes and ocaml does not.  You can't imagine how great type classes are
until you start using them.  They mirror so much of what you intend a
function to be in the type of the function, and they also allow you to use
functions in a much broader domain than you would expect.  Basically (if
you don't know this already), type classes give you overloading of
functions and operators on new data types, all done at compile time.
Ocaml, in contrast, has "functors" which allow you to create customized
versions of data structures parameterized on almost anything, but the data
structure you create is a full module.  This is also pretty elegant, but
IMO is much more heavyweight and doesn't have the syntactic advantages of
haskell's type classes.

Since ocaml doesn't have monads at all, a lot of the more advanced monadic
programming that haskellers like to do is much more cumbersome to do in
ocaml.

However, the biggest advantage that ocaml has over haskell is that for most
applications, an ocaml program will run faster, perhaps a lot faster, than
an equivalent haskell program (although the haskell program may be much
smaller, have fewer bugs, be prettier, etc.).  That's because lazy
evaluation in haskell requires the system to wrap closures around
unevaluated code and evaluate it later.  This has a cost, and it simply
isn't there in ocaml.

If you find functional programming *completely* baffling, learning haskell
off the bat is like jumping into the deep end of the swimming pool.
Learning a language like ocaml first can be a good stepping stone to
haskell, since many of the core concepts (like algebraic data types) are
basically the same.  Scheme is another great language to learn FP from,
preferably in conjunction with a good textbook like _How to Design
Programs_ (http://www.htdp.org) or _Structure and Interpretation of
Computer Programs_ (http://mitpress.mit.edu/sicp).  My progression was
scheme -> ocaml -> haskell, which was nice because I didn't have to add
nearly as much new material at each stage as I would have had to had I
learned e.g. haskell before learning scheme or ocaml.

Mike



> From: Marcin 'Qrczak' Kowalczyk <qrczak at knm.org.pl>
> Mail-Followup-To: haskell-cafe at haskell.org
> Date: Tue, 03 May 2005 20:38:14 +0200
> 
> John Goerzen <jgoerzen at complete.org> writes:
> 
> > I'd say that there are probably no features OCaml has that Haskell
> > lacks that are worth mentioning.
> 
> Its type system has some interesting features: polymorphic variants,
> parametric modules, labeled and optional arguments, objects, variance
> annotations of type parameters used for explicit subtyping.
> 
> It has more convenient exceptions: the exn type can be extended with
> new cases which look like variants of algebraic types.
> 
> There is camlp4 for extending the syntax or changing it completely.
> 
> OTOH Haskell provides type classes, better integrated arbitrary
> precision integer type, type variables with kinds other than *,
> polymorphic recursion, much better FFI, and with GHC extensions:
> universal and existential quantifiers in function types (OTOH OCaml
> recently got universal quantifiers in record fields), GADTs, implicit
> parameters, template Haskell.
> 
> -- 
>    __("<         Marcin Kowalczyk
>    \__/       qrczak at knm.org.pl
>     ^^     http://qrnik.knm.org.pl/~qrczak/
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 


More information about the Haskell-Cafe mailing list