caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Brian Hurt <bhurt@janestcapital.com>
To: Jonathan Bryant <jtbryant@valdosta.edu>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Amb
Date: Fri, 09 Feb 2007 16:55:24 -0500	[thread overview]
Message-ID: <45CCEDCC.1090200@janestcapital.com> (raw)
In-Reply-To: <1197BD5D-1373-4876-8DC1-FED84797E4A6@valdosta.edu>

Jonathan Bryant wrote:

> All,
>
> I'm having to learn Scheme for a class, and I ran across a simple but  
> really useful predicate: amb.  More info on this can be found here:  
> http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z- 
> H-16.html#node_chap_14
>
> Since OCaml doesn't seem to have this, I implemented it:
>
> exception Amb
> let rec amb l = match l with
> | [] -> raise Amb
> | h::t -> try h () with Amb -> amb t
>
> let amb l = try Some (amb l) with Amb -> None
>
You might try:
let rec amb = function
    | [] -> None
    | h :: t ->
       let r =
          try
             Some(h ())
          with
             | _ -> None
       in
       match r with
       | None -> amb t
       | Some(_) as e -> e
;;

As a slightly better implementation.

Brian


  reply	other threads:[~2007-02-09 21:55 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-09 21:31 Amb Jonathan Bryant
2007-02-09 21:55 ` Brian Hurt [this message]
2007-02-09 22:12   ` [Caml-list] Amb Andrej Bauer
2007-02-09 22:42     ` Tom
2007-02-10  0:03       ` Jon Harrop
2007-02-10  0:01   ` Jon Harrop
2007-02-10  1:12     ` Brian Hurt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=45CCEDCC.1090200@janestcapital.com \
    --to=bhurt@janestcapital.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=jtbryant@valdosta.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).