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 DAA02561; Sun, 26 Sep 2004 03:39:35 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id DAA01975 for ; Sun, 26 Sep 2004 03:39:34 +0200 (MET DST) Received: from ptb-relay01.plus.net (ptb-relay01.plus.net [212.159.14.212]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id i8Q1dXkE031007 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Sun, 26 Sep 2004 03:39:33 +0200 Received: from [80.229.56.224] (helo=chetara) by ptb-relay01.plus.net with esmtp (Exim) id 1CBO0h-000IEY-5t for caml-list@inria.fr; Sun, 26 Sep 2004 01:39:31 +0000 From: Jon Harrop 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 02:34:50 +0100 User-Agent: KMail/1.6.2 References: <20040925225246.48566.qmail@web53010.mail.yahoo.com> In-Reply-To: <20040925225246.48566.qmail@web53010.mail.yahoo.com> MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <200409260234.50929.jon@jdh30.plus.com> X-Miltered: at concorde with ID 41561DD5.000 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 haskell:01 inelegant:01 compile-time:01 metaocaml:01 higher-order:01 higher-order:01 ubiquitous:01 iostream:01 typename:01 endl:01 endl:01 vastly:01 verbose:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Saturday 25 September 2004 23:52, Vasili Galchin wrote: > I am reluctantly learning C++ STL (Standard Template Library) and the > notion of templates. Templates don't seem to be that great ... just > parametric plymorphism done in a somewhat heavy handed way when compared to > the same in OCaml, Haskell, etc. A beneficial (although inelegant) advantage of templates is the ability to partially specialise programs by performing arbitrary compile-time computation. This is not available in OCaml. This will be addressed (in a much more powerful and elegant way) by MetaOCaml just as soon as Taha at al. stop reading this mailing list and start working harder. ;-) > However, teh STL notion of containers and > available operations allowed on containers does seem to be be very powerful > and not available in OCaml. Is the last statement true? That last statement is only true in the absence of higher-order functions. Therefore, it does not apply to OCaml. In functional languages, like OCaml, the operations on containers are much more productively factored into higher-order functions which are then container-type independent. In particular, the ubiquitous map and fold functions. For example, to sum the floating-point elements of a container in C++, one might write: #include #include #include template double sum(IT begin, IT end) { double ans=0.; for (IT it=begin; it!=end; it++) ans +. *it; return ans; } int main(void) { list A; A.push_back(0.); A.push_back(1.); A.push_back(2.); A.push_back(3.); A.push_back(4.); vector B; B.push_back(0.); B.push_back(1.); B.push_back(2.); B.push_back(3.); B.push_back(4.); cout << sum(A.begin(), A.end()) << endl << sum(B.begin(), B.end()) << endl; } The OCaml equivalent would be: 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.|] If you have more time than sense then you'll prefer the C++ version (which is vastly more verbose and a bit quicker). If, on the other hand, you can see the forest for the trees then you'll want to be using OCaml because, given the same time, you can write OCaml programs which are both faster and smaller than equivalents written in virtually any other language. Not to mention that compilation of equivalent programs is typically between one and two orders of magnitude faster using ocamlopt rather than g++. 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: list::iterator new_end = remove_if(L.begin(), L.end(), compose2(logical_and(), bind2nd(greater(), 100), bind2nd(less(), 1000))); L.erase(new_end, L.end()); In OCaml: let l = List.filter (fun x -> not (100