Compilers

OCaml Transition Guide

This document is a transition guide for students in the Swarthmore CS curriculum. It contains examples of code written in Python, C++, and OCaml.

Variable Declaration

Note: the OCaml compiler is capable of some type inference, which allows it to guess which types you meant. The above can be written

let x = 5;;
let y = "Hello";;

You are free to use type inference as much as you want, but instruction in this course will avoid it (at least early on) to reduce confusion.

String Concatenation

Standard User Interaction

Function Declaration

Again, type inference can be used to write the above as

let sum_two x y = x + y;;

This course avoids relying on type inference especially in the case of functions, as the inference rules can be quite confusing to newcomers at first.

Recursive Functions

Loops

Technically, OCaml does have mutable variables and while loops. For this course, however, we will rely heavily upon our ability to think recursively and we will avoid those language features whenever possible. Much of the code we write will be much better expressed recursively!

Using Libraries

String Formatting and Printing

Exceptions

Tuples

Absent Values

Polymorphic Dictionaries