OCaml - List - Creating a list

As all programming languages, it is possible to create lists in OCaml. So, let’s see this, right now with new examples. We will indeed create two functions, one of ints and another of strings. For the ints: #let number = [1; 2; 3; 4];; For the strings: #let fruits = ["apricot"; "raspberry"; "cherry"; "tomato"];; As you can see, there is no need to tell to the interpreter the type of elements in the list. It’s automatic! 😊

February 29, 2012

OCaml - List - Iterating through a list and displaying all elements inside

As you already saw it, creating a list in OCaml is not so difficult. But this time, we will see how to iterate through a list and display all elements inside. We will create two arrays, one of ints, the other of strings. Once done, we will display their elements. Let’s see it with this OCaml list tutorial. Creating lists List of ints: #let number = [1; 2; 3; 4];; List of strings: ...

February 29, 2012

OCaml - Function - Creating a getter to retrieve an element of a tuple

OK, this is not really a getter implementation like we can have it in other object-oriented programming, but it is close of it. We have first to create an human variable with a tuple (“name”, age). Then we have to create two getter functions to retrieve the first and the second parameter of this human variable. Let’s begin by the human variable: # let human = ("John", 50);; val human : string * int = ("John", 50) Let’s continue by the getName function: ...

February 28, 2012

OCaml - Function - Creating an easy function

In this tutorial we will see how to create a function in OCaml language. It will be an easy function to understand how it works. We will create a function that returns an int + 1. Here the code: # let myFunc myVar = myVar + 1;; Notice that you can also create a function like this: # let myFunc = fun myVar -> myVar + 1;; Press enter, it will display: val myFunc : int -> int = ...

February 28, 2012

OCaml - Function - Using recursion

OCaml is a fully recursive language. So using recursion is completely natural. We will see in this example how to create an easy recursion of a classic factorial. This in two different manners. These two ways of using recursion are strictely the same, the type of the function and the result as well of course. The first manner to create a recursion function # let rec factorial n = if n = 0 then 1 else n * factorial(n - 1);; You can split it to have a better indentation: ...

February 28, 2012

OCaml - Utilities - Using the rlwrap command

When you use OCaml on Linux for example, you cannot by default using the arrow from your keyboard, neither the completion. If you try to use them you will have something like that: # ^[[A^[[D^[[C^[[B^[[D^[[A^[[C^[[D^[[B^[[C But there is readline wrapper named rlwrap that will help us in this task! Just write it before starting your OCaml interpreter, like this: $ rlwrap ocaml Thus your arrows will work and completion too.

February 27, 2012

OCaml - Variable - Creating variables

For creating variables in OCaml we need to use the let keyword. The syntax to create a variable is the following: # let myVar = 90;; When you type enter, it will be displayed the type of your variable: val myVar : int = 90 The OCaml interpreter will find the type by itself. This mechanism is called the inference. 😊

February 27, 2012

Android - MediaPlayer - Example of playing sounds

Maybe you are wondering how to play a MP3 sound by clicking a simple Button on Android. Eh, you know what? You are in the right place! Note that this tutorial for MP3 sounds works as well with MP3 musics and of course a lot of different types of format such as WAVE or MIDI. A complete list can be found on the official website of Android developpers: http://developer.android.com/guide/appendix/media-formats.html. After a presention of which MP3 we will need, we will show the source code needed for this tutorial. ...

December 23, 2011

Apache - Ant - Errors

You were compiling on Windows 7 when suddenly you are getting this error: [ERROR] com.sun.tools.javac.Main is not on the classpath. [ERROR] Perhaps JAVA_HOME does not point to the JDK. Then maybe something like that: [ERROR] It is currently set to "C:\soft\java32\jdk1.7.0\jre" So the JRE instead of the JDK? What’s going on? Ant needs a file named tools.jar that it searches in the JRE but this file is in the JDK! ...

December 8, 2011

Apache - Continuum - Configuration

After installing Apache Continuum continuous integration tool, you may want to configure it and of course, build your first project with Maven and Continuum. But you already have a project on SVN, and you wish to use it as well. No problem you are in the right place. Software used in this tutorial: Apache Maven 3 Apache Continuum 1.4.0 Subversion 1.7 Configuration of Continuum with Maven and SVN on Windows Prerequisites If you don’t have a project built with Maven, I suggest to follow this tutorial of “What is Maven?”. ...

November 30, 2011