The polymorphism is good and allows your code to be reused with greater

ease. Imagine you did so, and then applied the functor to produce a specific

module A where all the 'a are now some concrete type t. Now consider the

difference between A.combinator and the polymorphic combinator you already

have. The only difference is that A.combinator can be applied only to t, but

the polymorphic combinator can be applied to t's as well as any other type.

You only restricted your code.

Functors are a good choice when there is an operation that inherently

relates to 'a, and which another operation will always depend on. For

example, say you want to define the function min : 'a -> 'a -> 'a, which

takes two arguments and returns the smaller one. For this function to work

(ignoring that standard library already provides this) you need a method for

comparing values of type 'a. So you really need to make the function

signature be min : ('a -> 'a -> int) -> 'a -> 'a -> 'a. For a specific type,

say int, the first argument, call it cmp, will always be the same. If you're

going to take the min of two int's often, it will be burdensome and error

prone to have to always call min with same cmp as its first argument. Here

you can functorize to eliminate the need for the first argument.

On Jan 28, 2008 12:08 PM, Till Crueger <crueger@...-bonn.de>

wrote:

> Hi,

[Non-text portions of this message have been removed]

>

> I have a question that mostly concerns style. I am still not sure when to

> use functors and when to better avoid them. The last time I used them I

> needed them for consistency reasons, so I was pretty certain that they

> were the best choice. Now I am writing another module and so far I have

> the following signature in my .mli:

>

> type 'a combinator = 'a -> 'a -> 'a;;

> type 'a mutator = 'a -> 'a;;

> type 'a pop = 'a list;;

> type 'a fitness = 'a -> float;;

> type 'a t;;

>

> val evolve_by_fittness : 'a fittness -> int * 'a combinator -> float *

> mutator -> 'a t

> val perform : 'a -> 'a pop -> 'a pop;;

>

> So the 'a is used in every type and every value. This strikes me as a

> place where I could use functors to get rid of the 'a but then again I

> would have quite a few with declarations. How would you handle this

> problem?

>

> Thanks,

> Till

>

> --

> A person cannot do right in one department whilst attempting to do wrong

> in another department. Life is one indivisible whole.

> --Mahatma Gandhi

>

>

> Archives up to November 11, 2006 are also downloadable at

> http://www.connettivo.net/cntprojects/ocaml_beginners/

> The archives of the very official ocaml list (the seniors' one) can be

> found at http://caml.inria.fr

> Attachments are banned and you're asked to be polite, avoid flames etc.

> Yahoo! Groups Links

>

>

>

>