Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Easy solution in OCaml?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-04-25 (16:59)
From: Markus Mottl <markus@o...>
Subject: Re: [Caml-list] Easy solution in OCaml?
Siegfried Gonzi schrieb am Freitag, den 25. April 2003:
> Often I am contemplating whether it would be possible to use
> OCaml in combination with my beloved Bigloo to perform statistical
> evaluations. I am not sure whether there are any out there who /can/
> do this evaluations with OCaml what you normally would do with Matlab.

For heavy duty statistics you better use tailor-made libraries, e.g. the
OCaml-interface to the GSL (Gnu Scientific Library) OCaml-GSL:

possibly in combination with other linear algebra libraries, e.g. LACAML:

This should give you top-performance on really large data.

> The problem what arises: type system and working against the compiler.

Mandatory rule for OCaml-beginners: NEVER try to work against the
compiler. The compiler is your friend.

> Rationale: given a list of 12 month.  I would like to calculate the 
> quarterly means and skip any nan. Easy? Yes it is but only on paper and 
> in Scheme:

Or in OCaml, if you know how to do it elegantly and reasonably efficiently:

  let coll (len, sum as acc) n = if n >= 0 then len + 1, sum + n else acc

  let qmeans =
    let rec loop acc = function
      | a :: b :: c :: t ->
          let len, sum = coll (coll (coll (0, 0) a) b) c in
          loop ((if len = 0 then 0.0 else float sum /. float len) :: acc) t
      | [] -> List.rev acc
      | _ -> failwith "qmeans: illegal list" in
    loop []

> In Scheme I wrote it as functional as possible, but I fail to do this in 
> Ocaml. I mean doing it in OCaml via loops would be straightforward, but 
> I didn't succeed in coming up with a solution of:
> - relies on pattern matching?

Yes, you have to match the list.

> - is short and and shouldn't resemble imperative style

See above.

> - why if-then constructs? I think this was called "guards" in Clean?

I actually find it strange that there is no such thing like if-then-else
in Clean. OCaml also supports guards, but they are not always best style.

> - why begin-end constructs? In Scheme begin-end constructs are ordinary, 
> but I find it irritating to use it in OCaml.

You can use parentheses instead.

> - is it possible to give type information for readbility.

Yes, e.g.:

  let qmeans : int list -> float list = ...

Markus Mottl

Markus Mottl

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: