Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Re: [Caml-list] productivity improvement
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-07-18 (23:13)
From: Oleg <oleg_inconnu@m...>
Subject: Re: [Caml-list] productivity improvement
On Tuesday 16 July 2002 01:29 pm, Shannon --jj Behrens wrote:

> Wow, that's an impressive piece of C++!!!  C++ never
> seems to stop surprising me!  Nonetheless, I feel the
> OCAML version is infinitely more readable.
> Best Regards,
> -jj

I'd say, to a person equally familiar with O'Caml and C++, the readability 
ratio is less than 2 [1] The readability of compiler messages is a whole 
different story: G++ gives horrible messages when templates, or, god forbid, 
STL errors are present [2]

However, the C++ version looks more "extensible" to me: Suppose that in a 
while, you decide that you want your "node" to be not only Leaf or Unop or 
Binop, but also Triop:

type 'a node = Leaf of 'a | Unop of ('a->'a) * 'a node | Binop of ('a * 'a -> 
'a) * 'a node * 'a node | Triop of ('a * 'a * 'a -> 'a) * 'a node * 'a node * 
'a node;;

You will need to modify the original node type and function "eval" by adding 
an extra pattern to "match" statement, and then recompile everying that 
depends on it. At the same time, with C++ the type of node remains the same. 
You just need to derive a new class  from it:

template<class T>
struct triop : public node<T> {
      T (*fun)(T, T, T);
      node<T> &n1;
      node<T> &n2;
      node<T> &n3
      T eval() { return fun(n1.eval(), n2.eval(), n3.eval); }
      triop (T (*f)(T, T, T), node<T>& N1, node<T>& N2, node<T>& N3) : 
fun(f), n1(N1),  n2(N2), n3(N3) {}  

Recompiling isn't necessary. In fact, "old code" may call "new code" if you 
happen to pass it a node that happens to be a triop.


P.S. Having read the CalTech tutorial and chapters 1-4 & 14 of the O'Reilly 
book, and having gotten some experience with O'Caml, I'm running low on 
motivation right now. Please give me examples of what's 
hard/awkward/impossible in C++, but relatively easy in O'Caml, if any (I have 
only finite time, so 50 KLOC Coq is not a good example :)

P.P.S. My primary interest is statistical AI (artificial neural networks). I 
haven't found any relevant libraries or applications in SML or O'Caml. That 
is a bit discouraging.

[1] And the example was hand-picked!
[2] If one doesn't want "ad hoc" genericity, templates aren't necessary, of 
To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: