On Friday 04 May 2007 18:52, Fabrice Marchant wrote:

> Hi Dave !

>

> Thanks to have given the explanations I needed : very interesting.

> Sometimes I feel OCaml language seems very natural but it is not so easy

> that it appears at first glance.

Indeed. One gets the impression that INRIA use functors in the Set and Map

modules just for the sake of it. The advantage of using functors here is that

you can statically guarantee that you don't accidentally mix two types of set

that use different comparison functions. However, it is clearly quite tedious

to use in practice.

The F# programming language from Microsoft Research doesn't take this

approach. In fact, the language doesn't even provide functors. Consequently,

all sets are of the type 'a set and you have polymorphic functions to

manipulate them. So you can write:

> let map f s = Set.fold (Set.add << f) s Set.empty;;

val map : ('a -> 'b) -> 'a set -> 'b set

Note the use of the built-in function composition operator <<.

Personally, I believe that INRIA solved a small problem but left a big problem

in this aspect of their design. Specifically, their approach leaves you open

to accidentally applying structural functions to sets impicitly. For example,

if you try to use a set as the element type of a hash table in OCaml, your

program will break randomly. Or just doing s1 = s2 for sets is wrong.

F# fixes this by carrying comparison and hashing functions with all objects.

This adds a little overhead but it is always good to sacrifice performance in

favor of correctness.

F# is already more advanced than OCaml in many ways and also opens up Windows

programming. I highly recommend it for anyone interested in working with

OCaml.

--

Dr Jon D Harrop, Flying Frog Consultancy Ltd.

The F#.NET Journal

http://www.ffconsultancy.com/products/fsharp_journal/?e