An OCAml Example
So before really looking at syntax and other important parts of programming languages, most resources start by demonstrating a simple program in the new language. Let's do that to give you a sense of what the most fundamental parts of a program are, as well as what the compilation process might look like. So let's write the Hello, world! program! When we run it, the program will print a string to the terminal.
So let's first create the file where we write the program. python programs have the .py extension and C# programs have the .cs extension. OCaml programs, because they are in the ML family of languages, have the .ml extension. So open up the terminal and type the following command in the desired directory to create a new file and edit it in emacs.
emacs hello.ml
Next, we'll type our program into the file and save it.
Printf.printf "Hello, world!"
In order to compile our program into an executable, we type the following into the terminal:
ocamlc -o hello hello.ml
ocamlc is OCaml's batch compiler. The -o flag lets ocamlc that we want an output file. The output file will be called hello. The file that wants to be compiled is hello.ml.
We won't be delving to much into the actual compilation process, but here is more information on the compiler, the compilation process, makefiles, etc. if it is interesting to you.
Now let's run the actual program! We can do this with the following command at the terminal!
We won't be delving to much into the actual compilation process, but here is more information on the compiler, the compilation process, makefiles, etc. if it is interesting to you.
Now let's run the actual program! We can do this with the following command at the terminal!
hello
And the output should appear in the terminal as:
Hello, world!
Awesome! You've just written your first program! But since we usually want to write programs that aren't just Hello world, we'll take a look at expressions next. If OCaml programs are structures, expressions are the bricks that make them.