OCaml/Introduction

Basic Types
Every element OCaml has is associated with a specific type. The concept of types is a very powerful feature of OCaml, but for now we will just mention some predefined types: The formal names are what we call the types when we are talking with the OCaml system.
 * Strings (formal name:  example:  )
 * Integers (formal name:  example:  )
 * Floats (formal name:  example:  )
 * Booleans / Truth values (formal name:  example:  )

Hello World
Ok, let's see some action:

This will print the text "Hello World" on the screen.

Why do we write  and not just print? Well, actually there is no thing called just "print" in OCaml, but there is something called  (there are   and   too). The difference is most obvious when we print several things: If we do  followed by , the output will be: heyyou While if we do the same thing using  the output would be: hey you So when we use  every string is printed on the same line right next to each other, while with   every string gets its own line. could be read as "print this string and then end the line", so what this really means is that the next thing we print (regardless of how) will be on a new line, while the current string might or might not be on its own line depending on if there already is anything there.

Yeah, but how do I make that text actually print something on my screen?
So you're not satisfied with just reading what's happening? Want to see it for yourself? :-) You will have to install the OCaml development environment from here: official page, exactly what you install and how you use it depends on your computer and operating system. At the moment there are two components of interest to us - there's the compiler and the interactive environment. The compiler takes a text file and makes an executable of it that you can run just like any other program, and the interactive environment lets you communicate directly with the system. We will focus at the later as it is more suited for learning and experimenting.

Assignment

 * Install the OCaml development environment
 * Find out how to get the interactive system up and running

Got it running now? Inside the interactive system you will be presented with a prompt that looks like this: #. You communicate with the system by typing things after the prompt followed by  as well as enter. Hitting enter without the semicolons will let you continue on a new line without "sending" your message to the system. When the system receives the message it will parse and evaluate it and answer you with its version of the message. Let's try something, computers are quite mathematical beings so maybe it would like to talk about maths? Personally I like the number 5 so let's see what OCaml has to say about it:

A bit strange looking answer maybe? means that the type of our message was integer, and whats after the  means that the value of our message was. Not very useful, no? Lets try something else:

So, it can do something useful after all :-).

So is this some kind of interactive calculator or what?
It is a very powerful calculator mind you :-). And it is a lot more, but let's look at the calculator side of things a bit. Assume we want to now the answer of: "32 - (8 - 7 * 23) + 2 * (8 - 7 * 23)" (don't ask me why '^^). Notice how "8 - 7 * 23" appears two times? Feel lazy? I do, it is such a bother to type the same thing twice I'd do anything to avoid it. Fortunately I don't need to find my own typing-slave, because OCaml will come to my aid:

Nifty, isn't it? The important part is:  This will let us name an expression for use inside an other expression. We can also save something for later use:

This will let me use the name "my_favorite_number" to refer to 5 anytime.