From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id HAA06898; Sun, 26 Sep 2004 07:31:11 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id HAA09907 for ; Sun, 26 Sep 2004 07:31:10 +0200 (MET DST) Received: from mproxy.gmail.com (rproxy.gmail.com [64.233.170.202]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id i8Q5V94x018272 for ; Sun, 26 Sep 2004 07:31:10 +0200 Received: by mproxy.gmail.com with SMTP id 79so3077751rnk for ; Sat, 25 Sep 2004 22:31:09 -0700 (PDT) Received: by 10.38.15.36 with SMTP id 36mr591920rno; Sat, 25 Sep 2004 22:31:09 -0700 (PDT) Received: by 10.38.75.19 with HTTP; Sat, 25 Sep 2004 22:31:09 -0700 (PDT) Message-ID: <7f8e92aa04092522313d47820d@mail.gmail.com> Date: Sun, 26 Sep 2004 08:31:09 +0300 From: Radu Grigore Reply-To: Radu Grigore To: caml-list@inria.fr Subject: Re: [Caml-list] C++ STL and template features compared with OCaml parametric polymorphism and OO features In-Reply-To: <200409260234.50929.jon@jdh30.plus.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit References: <20040925225246.48566.qmail@web53010.mail.yahoo.com> <200409260234.50929.jon@jdh30.plus.com> X-Miltered: at nez-perce with ID 4156541D.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 2004:99 sizeof:01 sizeof:01 0.,:01 implemented:01 verbose:01 ocaml:01 ocaml:01 byte:01 writting:01 int:01 int:01 sep:01 45%:97 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Sun, 26 Sep 2004 02:34:50 +0100, Jon Harrop 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 #include #include #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 dv(ALLARRAY(ddata)); int idata[] = {0, 1, 2, 3, 4}; list 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 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