2011/10/5 David Baelde <[email protected]>:
> Hi Fabio,

Hi all!

> Using exceptions in that kind of code is not wrong, and it's more
> efficient. Without it, we would have to continue the folding over the
> rest of the list, even though we already know what we've found. By
> raising an exception, we interrupt that computation and jump directly
> to the handler.
>
> Exceptions in OCaml are very fast, pretty much a goto/longjmp. This is
> why (unlike in other languages) they can be used in interesting ways
> as a programming construct, and not only for errors / exceptional
> cases.

Not to be pedantic, but the truth is also that fold_left provides a
quick way to implement what you want, therefore we use exceptions to
make use of it.

You could do it without exceptions and with a tail-recursive function this way:

let rec f result list =
  match list with
      | x :: list' ->
           let result' = (something about x and result.. ) in
           if (for some reason I shall stop) then
             result'
           else
             f result' list'
      | [] ->
     (here it depends: maybe return result,
      or assert false, or..)
  in
  let result = f 0 list in
  (...)

If you are interested into this, we could continue that discussion on
-devl. In particular, tail-recursive functions are very important in
OCaml..

Romain

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure contains a
definitive record of customers, application performance, security
threats, fraudulent activity and more. Splunk takes this data and makes
sense of it. Business sense. IT sense. Common sense.
http://p.sf.net/sfu/splunk-d2dcopy1
_______________________________________________
Savonet-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/savonet-users

Reply via email to