What is OCaml?

Developed for more than 20 years at Inria by a group of leading researchers

 It's used in environments where a single mistake can cost millions and speed matters

 It has an advanced type system that helps catch your mistakes without getting in your way

Strength

A powerful type system

User-definable algebraic data types and pattern-matching

Automatic memory management

Separate compilation of standalone applications

https://try.ocamlpro.com/

let bidning

Note that there are some constraints on what identifiers can be used for variable names.

 

Punctuation is excluded, except for _ and ', and variables must start with a lowercase letter or an underscore.

The let syntax can also be used to define a function

Parametric polymorphism

Tuples

an ordered collection of values that can each be of a different type

Lists

hold any number of items of the same type

Some basic operations with List

Recursive list function

Removing sequential duplicates

Options

Nesting let with in

Records

field punning

Type

Variant type

Example of using variant type

Arrays

Mutable Record Fields

Create/Update Mutable Record

Text

Refs

Handful functions to work with Refs

How to implement Ref by yourself

For loop

While loop

Where Is the Main Function?

OCaml doesn't have a unique main function

Modules

In OCaml file is a module

Module Interfaces

A module defined by a file filename.ml can be constrained by a signature placed in a file called filename.mli.

Sub Modules

Including modules

Bytecode Versus Native Code

Bytecode is interpreted by a virtual machine

Native code can be run only on a specific OS and processor architecture

Configuring environment for OCaml

brew install ocaml
brew install opam
opam install ocamlbuild

opam switch <version>

opam switch list --all

opam pin add <package name> <target>

Let's build a small OCaml program

1. Create example.ml in some empty folder

2. ocamlbuild -use-ocamlfind -pkg core -r -tag thead example.byte

3. example.byte file will be generated which we can run now

OCaml

By acierto

OCaml

  • 1,306