Amir Chaudhry

thoughts, comments & general ramblings

OCaml - Installation and hello world

This post is part of a series where I'm trying to teach myself OCaml.
You might want to start at the beginning.

It’s been a few days into my OCaml experience so this is a write-up of what I’ve come across so far. I’ve spent more time reading background rather than getting stuck in so I’ve copied in some of the links that I’ve found interesting/useful at the end.

Installation

There are number of ways you can get OCaml on your machine. The most obvious would be to get the source via the release page, but you could also use something called GODI, which apparently bundles a bunch of other stuff alongside the language.

I don’t really want to install from source ‘by hand’ and I definitely don’t need all the stuff that comes with GODI. I happen to use Homebrew on my machine, so I checked to see if I can install that way. Turns out I can, so that’s what I ended up doing.

$ brew install objective-caml

The current version of OCaml is 4.00.0 and you can check the version you have installed by typing ocaml -version in your terminal.

notes

Be aware that Homebrew has it’s own installation process and depends on Ruby. To get Ruby, someone recommended that I install via Ruby Version Manager. To be able to do the above, you’ll need to have the OSX developer tools installed, which means having Xcode.

Hello World!

The first thing to do is get a hello world programme working. Since OCaml is a compiled language, that means writing the necessary source code into a file, compiling it and then executing it. In this case I only need one line that prints ‘hello world’ to the screen and I’m taking it from INRIA’s site (link).

print_string "Hello, world!\n";;

Take the one line above and save it in a file called hello.ml. Now we need to compile that file using the OCaml compiler. At the command prompt, type the following:

$ ocamlc -o hello hello.ml
$ ./hello

Since I don’t really understand the first line I should break it down. ocamlc is the command to invoke the compiler, the option -o hello means you want to name the output executable to be (in this case) ‘hello’ and the final argument is the source code file. It’s useful to look at the man page for ocamlc to see what other options are available. The second line executes the programme, which prints hello world to the screen with a line-break.

I also notice that I now have two other files in addition to the source, hello.cmi and hello.cmo. According to the man page, these are the ‘compiled interface’ and ‘compiled object code file’ respectively. I have no idea what that means but removing the files doesn’t affect the executable.

OCaml ‘toplevel’

Even though OCaml is a compiled language, there’s something called ‘toplevel’ that allows interactive use (more on toplevel). To enter this mode, you simply have to type ocaml at the prompt so let’s try running the above hello world program using toplevel.

$ ocaml
        OCaml version 4.00.0

# print_string "Hello, world!\n";;
Hello, world!
- : unit = ()
# #quit;;
$ 

The $ prompt is the command line and the # prompt is where toplevel is awaiting a new line of input. The input can span multiple lines and is terminated by ;; (as in the source code).

To exit toplevel, type #quit;;. It took me three attempts to get that right. I haven’t really played around with toplevel much and I think I’m likely to stick with source code and compiling until I’m a little more comfortable with the syntax.

That’s pretty much where I am for the moment and everything so far has been straightforward. Obviously, I should push myself a bit harder :)

Resources

This is the material I found and have been looking over for the last couple of days. Useful as background but I’d say I’m in danger of (semi-productive) procrastination if I’m not careful.