Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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
      done;
    done;
    raise Not_found

Rich.

-- 
Richard Jones
Red Hat