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

List Concatenation

Standard User Interaction

Function Declaration

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