Browse thread
Re: [Caml-list] productivity improvement
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| 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.
Oleg
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
course.
-------------------
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