Ocaml Cheat Sheet



  1. OCaml Cheat Sheet by advitya via cheatography.com/20757/cs/3643/ Example Module & Functor example (cont) Now, the functor does not have an abstract type. Because, the user can see the type in the functor. So, we have to hid that type t impele men tation. There's a problem with Makei nte rval. The invariant is enforced by the Icreate function.
  2. OCaml-Java Cheat Sheet Xavier Clerc, June 2015 Tools ocaml classical toplevel ocamlbuild compilation manager (ocamljava-aware) ocamlc compiler producing OCaml bytecode ocamldebug debugger for ocamlc-compiled programs ocamldep dependency analyzer ocamldoc documentation generator (ocamljava-aware) ocamlj toplevel using Java bytecode.

2 The OCaml interpreter The command ocaml (without parameters) launches the Caml interpreter. You can type Caml instructions inside and immediately get their result it is highly recommended to use the ledit command with ocaml as argument to make interacting with this inter-preter bearable. For instance, you can type: # let x = 2;; val x: int = 2. Ocaml interactive bytecode toplevel ocamllex.opt lexer compiler ocamlyacc parser compiler ocamldep.opt dependency analyser ocamldoc documentation generator. Earlier, we dusted-off our language and stdlib cheat-sheets. Because we wanted teachers to be able to give those cheats to their students as quickly as possible, we missed a few typos (special thanks to @hannes, @spop, @Khady, @holmdunc and narimiran for their keen eyes). With more time, we managed to design an opam cheat sheet we are proud of.

Ocaml Cheat Sheet Pdf

By Brendan Long

One of the hardest parts of learning OCaml is figuring out what the infix operators do, since they’re just a string of symbols and you can’t find them with a Google search. This is my attempt to make a cheatsheet for whenever you’re wondering what a random series of symbols means. Doing a search on this page should find basic info about any of the common OCaml operators.

Note that some libraries define their own operators, like how Jane Street’s Command.Spec defines ++, +>, and +<. In cases like that, hopefully the library you’re using will make it clear what the infix operators do.

In OCaml, a function is infix if its name starts with one of these characters:

Followed by zero or more of these characters:

When defining an infix function, you need to put () around the “name”.

For example, in utop:

Also, you can see the type of an infix operator in utop by again wrapping the function name in parentheses:

Sheet

The official documentation for this is here, although this blog has a more accessible explanation.

The built-in operators are defined in Pervasives:

Refer to the documentation for the magic involved in functions that work on multiple types (=, <>, <, >, etc).

Numbers

Jane Street generally defines arithmetic operators in modules where they make sense, so you can do things like:

The documentation for this interface is under Int_intf.S_common, although most of them are defined for floating point numbers too.

Monads

Ocaml Cheat Sheet 2020

Jane Street’s libraries (Core, Async, Base, etc.) consistently define infix operators under Monad_infix modules.

map and bind are documented assuming that you’re familiar with monads, and you may find this StackOverflow answer useful if you need more information.

Cheat

>>= and >>| show up most commonly in Async, but they can also be used with Option, List, Result, etc.

Lwt

See the Lwt documentation.

Lwt doesn’t have Async’s >>=? or >>|? because Lwt.t can contain errors without having a separate Or_error module.