stumped
stumped

Reputation: 3293

How to import a module to use within a module (ocaml)

I'm new to functional programming and was wondering what would be the best way to do this. I have one file (display.ml) that has functions that saves, loads, and displays the high score, and another file (turn.ml) that has functions to handle playing a turn in a card game. I'm currently trying to tie both of these together in one file called game.ml. I made a module in display.ml and turn.ml, whose contents contains all the functions I previously wrote in the file. I was wondering what would be the best way to import these modules into game.ml. I was reading this, and it seems like the modules have to be defined in the same file that you would like to use it in

Upvotes: 2

Views: 1083

Answers (1)

V. Michel
V. Michel

Reputation: 1619

Suppose you have three files :

cat display.ml
let p x = Printf.printf  "%d" x

cat turn.ml
let t =(+) 1

cat game.ml
open Display
open Turn   
let ()=
  p (t 2 )

As you can see, you can "import turn.ml" with open Turn and use the function t inside game.ml

To compile :ocamlbuild game.byte
Exexcution :./game.byte
3

Upvotes: 3

Related Questions