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:
- Strings (formal name:
"this is a string")
- Integers (formal name:
- Floats (formal name:
- Booleans / Truth values (formal name:
The formal names are what we call the types when we are talking with the OCaml system.
Ok, let's see some action:
print_endline "Hello World"
This will print the text "Hello World" on the screen.
Why do we write
print_endline and not just print? Well, actually there is no thing called just "print" in OCaml, but there is something called
print_string (there are
print_float too). The difference is most obvious when we print several things: If we do
print_string "hey" followed by
print_string "you", the output will be:
While if we do the same thing using
print_endline the output would be:
So when we use
print_string every string is printed on the same line right next to each other, while with
print_endline every string gets its own line.
print_endline 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.
- 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:
# 5;; - : int = 5
A bit strange looking answer maybe?
int means that the type of our message was integer, and whats after the
= means that the value of our message was
5. Not very useful, no? Lets try something else:
# 5 + 2 * 3 - 1;; - : int = 10
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:
# let a = 8 - 7 * 23 in 32 - a + 2 * a;; - : int = -121
Nifty, isn't it? The important part is:
let .. = .. in ... This will let us name an expression for use inside an other expression. We can also save something for later use:
# let my_favorite_number = 5;; val my_favorite_number : int = 5
This will let me use the name "my_favorite_number" to refer to 5 anytime.