Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: [Caml-list] OCaml popularity
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Brian Hurt <brian.hurt@q...>
Subject: Re: [Caml-list] OCaml popularity
On Thu, 13 Mar 2003, Karl Zilles wrote:

> Daniel M. Albro wrote:
> > imperative side of the language will get fleshed out a bit with some
> > expanded loop features like break statements, 
> 
> You can always implement a break statement as an exception:
> 
> exception Break;;
> 
> try for i = 1 to 10 do
> 	if i == 5 then raise Break
> done with Break -> ();;
> 
> As an exception, you could (if you wanted) include special case handling 
> for the break, or create a more aptly named exception and pass through 
> data.

How efficient is this vr.s rewritting the loop as a tail recursion?  I.e.:

    let rec loop idx = 
        if (idx == 10) then () (* exit the loop *)
        else if special_case(idx) then () (* early exit of the loop *)
        else
        begin
            foo(idx);
            loop (idx + 1)
        end
    in
    loop 1

Naturally, instead of returning unit, the function can return whatever.
All for loops can be rewritten as while loops, and all while loops can be
rewritten as tail recursive functions.

What I'm worried about is the costs of throwing an exception.  This isn't
C++ (where throwing an exception needs to unroll the stack, calling all
the correct destructors, and can therefor be quite expensive), but that
doesn't mean exceptions are 'cheap'.  Exceptions, by definition, are
exceptional.  Were I writting the optimizer, I'd freely assume that any
code path that ends in throwing an exception is not a critical path and 
can be slow.

Brian


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners