English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] C++ STL and template features compared with OCaml parametric polymorphism and OO features
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-09-26 (18:56)
From: Jon Harrop <jon@j...>
Subject: Re: [Caml-list] C++ STL and template features compared with OCaml parametric polymorphism and OO features
On Sunday 26 September 2004 15:36, skaller wrote:
> On Sun, 2004-09-26 at 23:05, Jon Harrop wrote:
> >  If you have HOFs, you
> > just write my "sum" function and get on with something more interesting.
> Nope. That only works for a limited class algorithms.
> Try a map function.. woops, you can't type it .. :)

Yes you can:

# let vec_add map2 = map2 ( +. );;
val vec_add : ((float -> float -> float) -> 'a) -> 'a = <fun>

So you can then do:

# let list_add = vec_add List.map2;;
val list_add : float list -> float list -> float list = <fun>

I think you're thinking of a function to convert between 'a and 'b container 
types. This can be achieved in general by folding an insertion function using 
the fold of the input container and the insertion of the output container. As 
I've said recently, doing this in the completely generic case buys you 
nothing but writing *_of or of_* functions does, for example a list_of 
function which accepts a fold and container of any type:

# let cons h t = h::t;;
val cons : 'a -> 'a list -> 'a list = <fun>
# let list_of fold_right c = fold_right cons c [];;
val list_of : (('a -> 'a list -> 'a list) -> 'b -> 'c list -> 'd) -> 'b -> 'd 
= <fun>

Then you have:

# let list_of_array a = list_of Array.fold_right a;;
val list_of_array : 'a array -> 'a list = <fun>

> This is somehow like the covariance problem: things
> kind of work for one variable by cheating, but once
> you have several it fails.

You mean there is a trade-off of functionality between more- and less-capable 
type systems?

> Accumulate can accept a function object argument.
> C++ DOES have higher order functions.

That statement is so HOF-definition dependent that it boils down to "C++ is 
Turing complete".

> Ocaml run time can also support polymorphic higher
> order functions -- but the type system as of
> Ocaml 3.04 could not. 3.08 can support them with
> the same problem as C++: messy housekeeping
> is required -- you have to put them in a record
> or class. [This is not so good because records
> and variants are generative/nominally typed like C++ classes
> .. Ocaml classes are actually algebraic .. LOL!]

I don't follow. List.fold_left is a polymorphic HOF that's been around for 
donkey's years, AFAIK.

> Sure but Ocaml offers other advantages such as type inference
> lacking in C++ that make code more concise -- as well
> as nice scoping constructs, lexical scoping, higher
> order functions, variants, and garbage collection.
> [Did I miss something .. ? :]

Good looking programmers.


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