Tugger the SLUGger!SLUG Mailing List Archives

Re: [SLUG] Your top-ten linux desktop apps


O Plameras wrote:

> I do not know O'Caml, so I just want to ask the equivalent of ff code.

Probably the best place to get an idea of the language is the
Pleac project:

    http://pleac.sourceforge.net/pleac_ocaml/index.html

> I can RTFM but if I can see the  equivalent of this code, it'd be helpful.
> I wish to have a quick idea of the language.

O'Caml is not a general replacement for C. It doesn't have
pointers for the same reasons Java doesn't have pointers.
You also can't realistically write a Linux device driver 
in O'Caml (although there is a project that attempts this
I personally think its a bad idea.).

The O'caml version of your program therefore doesn't do the
pointer part and reduces to:

    let integer_array = [| 1 ; -2 ; 3 ; -4 ; 5 ; -6 ;
        -7 ; 8 ; -9 ; 32727000 |] ;;

    Array.mapi (fun i x
        -> Printf.printf "integer_array[%d] = %d\n" i x
        ) integer_array ;;

The first part (let ...) is pretty obvious. The second part
probably looks odd to someone who has only programmed in
C, C++ and java. It creates an annonimous function which takes
two integer parameters (i and x) and then mapps that function
onto the array. The anonymous functions gets called once for
each element in the array passing the array index and value
each time.

You will notice that something like the Array.mapi function is
much less likely to contain errors than the C for loop.

Erik
-- 
+-----------------------------------------------------------+
  Erik de Castro Lopo
+-----------------------------------------------------------+
"I consider C++ the most significant technical hazard to the survival
of your project and do so without apologies." -- Alistair Cockburn