Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Radu Grigore <radugrigore@gmail.com>
To: caml-list@inria.fr
Subject: Re: [Caml-list] C++ STL and template features compared with OCaml parametric polymorphism and OO features
Date: Sun, 26 Sep 2004 08:31:09 +0300	[thread overview]
Message-ID: <7f8e92aa04092522313d47820d@mail.gmail.com> (raw)
In-Reply-To: <200409260234.50929.jon@jdh30.plus.com>

On Sun, 26 Sep 2004 02:34:50 +0100, Jon Harrop <jon@jdh30.plus.com> wrote:
> For example, to sum the floating-point elements of a container in C++, one
> might write:

This example is slightly unfair to C++. In the OCaml code:

> let sum fold_left c = fold_left ( +. ) 0. c
> sum List.fold_left [0.; 1.; 2.; 3.; 4.]
> sum Array.fold_left [|0.; 1.; 2.; 3.; 4.|]

...you use _two_ library functions (namely List.fold_left and
Array.fold_left). Surely you should at least use one for C++ :)

--- begin code ---
#include <vector>
#include <list>
#include <numeric>
#define ALLARRAY(a) (a), (a) + sizeof(a)/sizeof((a)[0])
#define ALL(c) (c).begin() (c).end()

int array[] = {0, 1, 2, 3, 4}; 
double ddata[] = {0., 1., 2., 3., 4.}; vector<double> dv(ALLARRAY(ddata));
int idata[] = {0, 1, 2, 3, 4}; list<int> il(ALLARRAY(idata));

int as = accumulate(ALLARRAY(array));
double ds = accumulate(ALL(dv));
int is = accumulate(ALL(il));
--- end code ---

The resulting C++ code is still uglyer than its OCaml counterpart. And
longer. But you should notice that the same accumulate function is
used for three different containers, while in OCaml you use one
function for each container type. OTOH, something that is maybe less
obvious in the code above, I do use conainer specific functions in
C++: namely begin and end.

So, in short: In OCaml you don't have iterators so the fold method is
container specific. In C++ you have iterators that allow a
container-independent "fold-like" function to be implemented. The C++
program is more verbose.

> Believe it or not, the previous example favours C++ more than most others.
> Check out this example of function composition from SGI's STL manual:

Using C++'s <functional> header (or Boost.Lambda for that matter) is
sure to give you a headache after programming a bit in a functional
language like OCaml. But the same can be said about writting
imperative code in OCaml.

I have recently compared two implementations of the same small program
in C++ and OCaml, both written by me. The OCaml one was 45% the size
of the C++ one (byte count). After compression (with bzip2) it was
67%. And it was kind of imperative job so C++ should have been in
advantage there. And I know C++ much better than OCaml, so this should
have been another advantage..

regards,
 radu
PS: ok, maybe it's unfair to ocaml now that I used macros..

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


  reply	other threads:[~2004-09-26  5:31 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-09-25 21:12 Vasili Galchin
2004-09-25 21:38 ` Nicolas Cannasse
2004-09-25 22:15   ` Vasili Galchin
2004-09-25 22:52     ` Vasili Galchin
2004-09-26  1:34       ` Jon Harrop
2004-09-26  5:31         ` Radu Grigore [this message]
2004-09-26  9:47           ` sejourne_kevin
2004-09-26 13:05           ` Jon Harrop
2004-09-26 14:36             ` skaller
2004-09-26 15:08               ` sejourne_kevin
2004-09-26 15:27                 ` skaller
2004-09-26 18:51               ` Jon Harrop
2004-09-26 20:14                 ` Radu Grigore
2004-09-27  1:59                   ` Jon Harrop
2004-09-27  4:48                     ` skaller
2004-09-27  9:40                       ` Jacques GARRIGUE
2004-09-27 10:50                     ` Radu Grigore
2004-09-27 12:14                       ` skaller
2004-09-27 13:11                       ` Jon Harrop
2004-09-27 13:31                         ` Radu Grigore
2004-09-27 16:54                           ` Jon Harrop
2004-09-29 18:59                             ` Radu Grigore
2004-09-27 13:32                         ` Radu Grigore
2004-09-27 14:04                         ` Brian Hurt
2004-09-27 14:58                           ` skaller
2004-09-27 15:30                             ` Brian Hurt
2004-09-27 16:38                               ` skaller
2004-09-27 17:01                                 ` Brian Hurt
2004-09-28  1:21                                   ` skaller
2004-09-27 16:41                           ` brogoff
2004-09-28  0:26                             ` skaller
2004-09-29 15:32                         ` Florian Hars
2004-09-29 16:49                           ` [Caml-list] Factoring HOFs [was Re: C++ STL...] Jon Harrop
2004-09-30  9:19                             ` Radu Grigore
2004-09-30 10:13                             ` Keith Wansbrough
2004-09-30 10:31                               ` Keith Wansbrough
2004-09-30 13:21                               ` skaller
2004-09-30 23:17                               ` [Caml-list] Factoring HOFs Jacques Garrigue
2004-10-01  8:46                                 ` Keith Wansbrough
2004-10-01 17:35                                 ` brogoff
2004-09-26 20:43                 ` [Caml-list] C++ STL and template features compared with OCaml parametric polymorphism and OO features skaller
2004-09-26 14:19           ` 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=7f8e92aa04092522313d47820d@mail.gmail.com \
    --to=radugrigore@gmail.com \
    --cc=caml-list@inria.fr \
    /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