Frink Léo
Frink Léo

Reputation: 121

Ocaml #load without top level?

I have a problem with ocaml, i'm a beginner in ocaml. I want to use #load "....ml" . Then open "....ml" .

When I'm using emacs it's ok but I currently using linux xfce with ocaml compiler and when I try to use #load and open, I have a syntax error.

I have already look on the web, if I didn't need to use '#' before load but I have also a syntax error.

Example of my code which work with Emacs :

#load "list_ap1.cmo"
open List_ap1;;

And there is what is it in liste_ap1.cmo

module List_ap1 =
  struct
    let len(l) = List.length l;;

let fst(l) =
  match l with
    [] -> failwith "error empty list"
  | hd::tail -> hd
;;

Can anyone help me ? Have a nice day

Upvotes: 2

Views: 430

Answers (1)

Pierre G.
Pierre G.

Reputation: 4441

As mentionned in toplevel, #load is for loading a bytecode file, which means that you have already compiled a ocaml source to bytecode using ocamlc, and in that case, you will load a file with extension .cmo or .cma. The leading # means that it is a command meant for the toplevel environment, and it is not a ocaml keyword.

open is a keyword to open a ocaml module (see modules) meant for structuring ocaml code.

To launch an ocaml toplevel environment, and not the compiler, just launch : ocaml , you will be able to invoke #load. Once List_ap1 is loaded, you can use its function by writing the full path : List_ap1.len . Or if you open this module, you name directly the function without the module name as prefix.

Upvotes: 2

Related Questions