tags:

views:

28

answers:

1

The aim of the module described below is to implement a module which once initiated by an integer n does all the operations based on the value of n.

module ReturnSetZero =
functor ( Elt : int ) ->
    struct
        let rec sublist b e l =
               match l with
           [] -> failwith "sublist"
          | h :: t ->
              let tail = if e = 0 then [] else sublist (b - 1) (e - 1) t in
                    if b > 0 then tail else h :: tail
        let rec zerol = 0:: zerol
        let zeron = sublist 0 n zerol
                (*other operations based on n which is selected once when the module is initialized*)
    end;;

Error: Unbound module type int

What is the issue here? Is there an alternate implementation which is more effective/intuitive?

+2  A: 

A functor maps modules to modules. An integer is not a module, so you cannot use it to as a functor parameter.

You need to define a module type:

module type WITH_INTEGER = sig
  val integer : int
end

module PrintInteger = 
  functor (Int:WITH_INTEGER) -> struct

  let print_my_integer () = print_int Int.integer

end

Of course, unless your module needs to expose types that are dependent on the value of the integer (or you have to expose a lot of values dependent on that integer), you're probably better off with a plain function that takes that integer as an argument:

let my_function integer = 
  let data = complex_precomputations integer in 
  function arg -> do_something_with arg data

This lets you run the complex pre-computations only once on the integer (when you pass it to the function).

Victor Nicollet