OCaml Tutorial. This is our first installment of exclusive tutorial Maldita Ocaml internet, a programming language that combines functional, procedural, objects and more. In the first installment basics of language, basic types, some functions to start playing and the link to distributions.
Introduction to Ocaml Tutorial
Introduction and History: Ocaml is a derivative of ML, ML is a functional programming language created by Robert Milner in 1975, the language ML itself was created as a prototype and soon the various programming languages came off among which stood out Haskell and CAML. Caml suffered several changes throughout its history, in 1985 Caml-Light and OCaml is created, the two versions more widespread today, standardizing the language and taking it to its final form. Ocaml is an extremely modern language, the last modification to the core of the language was made in 2000 with the addition, for example, variants anonymous types
Regarding the language itself is Ocaml, being a derivative of ML, a functional language; which means that it is possible to perform complex and comprehensive programs without using procedural programming, cycles or assignment of variables, everything can be done through functions. Moreover Ocaml is also a procedural language and for defining variables, using cycles and do everything common in languages like C or Pascal. Ocaml is a language of type "strict" meaning that the compiler is extremely picky about datatypes and does not allow compile programs in which there are inconsistencies in the types. Caml is also an object-oriented language incorporating classes, inheritance and polymorphism. Finally Ocaml is a "safe" language, which means that once compiled a program if it does not go into loop then ensures that the program will end with some sort
Result, there are no errors pointers, memory mishandling of invalid assignments, vector out, range, etc. The compiler specifically aims therefore to make the compilation is very demanding in terms, errors which lengthens the development time and significantly shortens debugging time. In this regard Ocaml. It is similar in some ways to ADA.
Ocaml super-basic: Let's start this introduction with a basic approach to language. First a program in Ocaml can either compiled or tested in interactive mode, in interactive mode Ocaml shows a prompt where you can enter expressions, functions, etc and testing. All our examples will be based in principle on the interactive mode, which is accessed by running the executable version of windows or running "./ocaml" in the version for Unix. In Ocaml sentences end with ";;" (double semicolon) so our first program will be the classic:
print_string ("Hello World") ;;
As we can see the result is "Hello World" it is displayed as the program output.
Data types: strings, chars, integers, floats and boolean: in Ocaml types of basic data of any language are used. There is also the type "unit" similar to the "void" of C which is a type that does not have type. Ocaml is, as we said, very restrictive and any type of operation that combines types will be penalized incorrectly. For example the usual arithmetic operations are +, -, *, / are replaced by +, -.., * And /.. if you want to use floats.
Continuing with the basics: The following program shows how to assign a variable and how to create a function, two basic tasks.
let x = 5 ;; let square x = x * x ;;
In the example we assign the variable "x" the value "5" (integer) and define the square function which raises a number to the square, to create the function will see something of the form: val square -> = informs us that square is a function that takes an integer and returns another. If we try to use it with a floating we will have a type mismatch error. The "let" construction has more than curious applications we will not comment now not to scare readers, just promise you interesting things for later.
Functions can be defined recursively using the keyword "rec" for example the following function calculates Fibonacci numbers
let rec fib n = if n <2 then 1 else fib (n-1) + fib (n-2) ;;
If you do not use the word rec we can not call into fib fib
In Ocaml functions can be passed as parameters to other functions and even a function can return a function result, we introduce in this way the data type "function" in Ocaml is simply a data type more. For example we can define the composition of functions of the form:
compose let f g = fun x -> f (g x) ;;
Here we define the composition of f and g saying that is a function of x, where x f and g are worth the result of applying returns
f to g (x). We can try doing:
compose let f g = fun x -> f (g x) ;; let sq = x * x ;; let incr = x + 1 ;; let ci = incr sq compose ;; ci 8 ;;
81 as a result returns as ci is a function that makes up the increase and power, which makes ci is to receive 8 sq (incr 8) or is (8 + 1) * (8 + 1). The ability to manipulate functions of Ocaml is one of the most powerful features of the language.
But let on: The let construction allows associating values to variables or function names bodies have the characteristic of being nestable, for example:
let x = 9 ;; and let = 8 ;; let x = 2 in 1 in let y = x + y ;; x + y ;;
Here the first x + y returns 3 but the second returns 17, what we did was create special departments of x and y for the execution of the sum, these "new" hidden variables x and y to the above but only worth within the "scope" scope in which they were defined.
Pattern matching: Another important feature of Ocaml is the mechanism of "pattern matching" that defines functions by different things according to the value they receive is the equivalent of a "case or switch" in language "C" except in a functional language that is elegant while the procedural languages is rather sloppy and an object-oriented language directly wrong. This is where Ocaml is a lot like Lisp and its traditional function "cond." Here is an example of how to use pattern matching function that calculates the Fibonacci numbers.
let rec fib n = n match with 0 -> 1 | 1 -> 1 | j -> fib (j-1) + fib (j-2) ;;
Which can be read as: if n matchea with 0 then I return 1 if n matchea 1 then I return 1 if n matchea with j (ie anything other than 0 or 1 and assign j) then I return fib ( j-1) + fib (j-2). As we can see the syntax "match" is simple.
When the body is a match expression is valid the following alternative syntax:
let rec fib n = function 0 -> 1 | 1 -> 1 | j -> fib (j-1) + fib (j-2) ;;
In the following example we construct a function that says if a number is even
is_even let x = x mod 2 match with 0 -> true | 1 -> false ;;
The idea is simple, if x mod 2 is 0 then it is true that the number is even and it is false. Interestingly this
example give us a "warning" compiler used to introduce several issues: first it is fundamental. not ignore the warnings, must always correct. In this case the warning is that the function receives an integer and It is not referred to the case in which the number is negative. To this we can add another alternative to that pattern, says if you receive anything else an error is returned.
is_even let x = x mod 2 match with 0 -> true | 1 -> false | _ -> raise (parameter_invalid "is_even") ;;
Here we see the use of "_" underscore indicate a pattern that allows matchea anything matcheada not so far (defaulting), and also see how to generate an error by raising an exception, Ocaml like Java handles all errors in time execution by using exceptions, subject to be discussed later
Useful links: The following link may get Windows or Linux versions of Ocaml to start learning the language.
Below: In our next installment we will discuss more advanced data types of Ocaml as tuples, lists and the concept of polymorphism Ocaml. Nothing like exercise is a good idea to define functions recursively and using patterns and try different things and get used to the environment par. Good luck.