3

I'm new to this language/Tecnology I have a simple question but I can not find answer:

I would like to create a my Module where you can enter OCaml simple functions / assignments such as the following

let rec gcd (m, n) = if m = 0 then n 
   else gcd (n mod m, n);; 
 
let one = 1;; 
let two = 2;; 

Use these functions to other programs OCaml

daniele3004
  • 13,072
  • 12
  • 67
  • 75
  • see also http://stackoverflow.com/questions/22028151/separate-compilation-of-ocaml-modules – Str. Sep 27 '14 at 21:58

1 Answers1

2

Every OCaml source file forms a module named the same as the file (with first character upper case). So one way to do what you want is to have a file named (say) numtheory.ml:

$ cat numtheory.ml
let rec gcd (m, n) = if m = 0 then n 
   else gcd (n mod m, n)

let one = 1
let two = 2

This forms a module named Numtheory. You can compile it and link into projects. Or you can compile it and use it from the OCaml toplevel:

$ ocamlc -c numtheory.ml
$ ocaml
        OCaml version 4.01.0

# #load "numtheory.cmo";;
# Numtheory.one;;
- : int = 1
# Numtheory.gcd (4, 8);;
- : int = 8

(For what it's worth, this doesn't look like the correct definition of gcd.)

Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108