Version française
Home     About     Download     Resources     Contact us    
Browse thread
How to do this properly with OCaml?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Christophe Dehlinger <christophe.dehlinger@i...>
Subject: Re: [SPAM_PROBABLE] - [Caml-list] How to do this properly with OCaml? - Bayesian Filter detected spam
Thomas Fischbacher wrote:

> Please don't tell me that "well, OCaml is not Lisp, after all".
> This I already know. But is there no proper way to get around that
> (technically speaking) unnecessary extra level of indirection that is
> forced upon me due to static type checking?
>
Hi Thomas,

What you're trying to build looks a lot like Extlib's dynamic 
(resizable) arrays.
I gave their implementation a cursory look: it seems to rely heavily on 
the Obj module.
So I guess there probably is no clean and efficient way to do this.

Of course, it is pretty awkward having to scan for the first element in

> the first nonempty array in an array of arrays, especially as that 
> element
> really does not play a that special role.
>
> Could anyone please tell me how to do all this in a more appropriate way?
>
Here's imho a more elegant way to flatten arrays :

let flatten arar =
  let cur_length = ref (-1) in
  let ai = ref (-1) in
  let i = ref (-1) in
  let total_length = Array.fold_left (fun accu ar -> accu + 
(Array.length ar)) 0 arar in
  let initfun _ =
    incr i ;
    while !i >= !cur_length do
      incr ai ;
      cur_length := Array.length arar.(!ai) ;
      i := 0
    done ;
    arar.(!ai).(!i) in
  Array.init total_length initfun;;
val flatten : 'a array array -> 'a array = <fun>
# flatten [| [|1;2;3|]; [|4|]; [| |]; [|5; 6|] |] ;;
- : int array = [|1; 2; 3; 4; 5; 6|]

However, this is very different from the first problem, as
- you know at the time of creation of the array both its length and its 
contents
- its length will not change after its creation


I have my own array-related problem, so I'll plug it here :)
A couple of times, I would have liked to build arrays of functions whose 
body uses the array itself, like:

#let rec foo = Array.init 5 (fun n () -> if n=0 then 0 else foo.(n/2) ())
 This kind of expression is not allowed as right-hand side of `let rec'

Why is this kind of construct forbidden ?

Cheers,
Christophe



__________________________

Ce message (et toutes ses pièces jointes éventuelles) est confidentiel et établi à l'intention exclusive de ses destinataires. Toute utilisation de ce message non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. L'IFP décline toute responsabilité au titre de ce message.

This message and any attachments (the message) are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited. IFP should not be liable for this message.

Visitez notre site Web / Visit our web site : http://www.ifp.fr
__________________________