UP | HOME

Support via Liberapay

OCaml Code Blocks in Babel

Babel support for OCaml

Introduction

OCaml is a statically-typed, functional programming language, similar to F#, Haskell, Scala, Rust, and Standard ML.

Requirements and Setup

Babel support for OCaml requires a working installation of the OCaml platform, which combines the OCaml compiler with a coherent set of tools, documentation, libraries, and testing resources.

Babel currently evaluates OCaml code with the OCaml interpreter and not the compiler.

It requires tuareg-mode, which uses a constant string to name the default interpreter session *OCaml*.

To configure OCaml code blocks in Org mode, add the appropriate dotted pair to org-babel-load-languages:

(org-babel-do-load-languages
 'org-babel-load-languages
 '((ocaml . t))) ; this line activates OCaml

Babel Features for OCaml Code Blocks

Babel supports OCaml sessions, which run in a buffer named *OCaml* by default. The header argument :session can only be one of "none", "default", or "*OCaml*", other values raise an error.

There are no OCaml-specific header arguments.

Examples of Use

Here is the obligatory "Hello World!" example.

#+begin_src ocaml
"Hello World!"
#+end_src

#+RESULTS:
: Hello World!

To return only the value, use the default header argument :results value. Note the double colon separating lines of code.

#+begin_src ocaml :var y=10
  let square x = x * x;;
  square y
#+end_src

#+RESULTS:
: 100

To return the value and its type, which is conventional in OCaml, set the header argument :results verbatim. Note that the square function persists in the interpreter session.

#+begin_src ocaml :var y=10 :results verbatim
  square y
#+end_src

#+RESULTS:
: - : int = 100

Documentation from the orgmode.org/worg/ website (either in its HTML format or in its Org format) is licensed under the GNU Free Documentation License version 1.3 or later. The code examples and css stylesheets are licensed under the GNU General Public License v3 or later.