[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Brian Hurt <bhurt@j...> |
| Subject: | Re: [Caml-list] Amb |
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