English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
break and continue for OCaml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-04-10 (13:39)
From: Richard Jones <rich@a...>
Subject: Re: [Caml-list] break and continue for OCaml
On Thu, Apr 10, 2008 at 10:59:16AM +0900, Sanghyeon Seo wrote:
> What do you think?

When you've done that, how about a type-safe return statement?  It
should immediately return from the inner-most function, allowing one
to return a value.

Here's a usage scenario (modified from Extlib, it would be even
shorter with 'break'):

  (* Find the index of string 'sub' within 'str' *)
  let find str sub =
    let sublen = String.length sub in
    if sublen = 0 then return 0;

    let len = String.length str in
    for i = 0 to len-sublen do
      let j = ref 0 in
      while String.unsafe_get str (i + !j) = String.unsafe_get sub !j do
        incr j;
        if !j = sublen then return i
    raise Not_found


Richard Jones
Red Hat