Version française
Home     About     Download     Resources     Contact us    
Browse thread
Objective Caml 2.02
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jean-Francois Monin <JeanFrancois.Monin@c...>
Subject: Re: List.filter in Ocaml 2.02
Alexey Nogin wrote :
 > > exception Identity
 > >
 > > let share f x = try f x with Identity -> x
 > >
 > > let filter f l =
 > >  let rec fil = function
 > >    | [] -> raise Identity
 > >    | h :: t ->
 > >       if f h then h :: fil t else share fil t in
 > >  share fil l
 > [...]
 > 2) Even if the list is not changed, you first allocate the space and put
 > its copy there, then raise an exception and return the old value. That
 > means that you first waste time in memory allocation and then you waste
 > it in garbage collection.

I don't agree, because this construction 
 > >       if f h then h :: fil t else share fil t in
                         ^^
should never be evaluated when Identity is raised in the "fil t"
occurring just after.  At least, if you replace "h :: fil t" with
"h $$ fil t", where

let ($$) x l = x :: l

you get the expected behavior (for mem alloc concerns).
It would be very surprising that the above program behaves not the same.

-- Jean-Francois