From: blue s. <blu...@gm...> - 2008-05-22 07:06:58
|
On 5/21/08, David Teller <Dav...@un...> wrote: > What should it do if there is more than one element in the enumeration ? > Raise an error or just consume the first element ? If think consuming the first element only is the saner decision. It's the strategy used by monadic sums, and the C-ish bool_of_int translation (0 -> false, everythin else -> true, so there is an information loss). Besides, it totally makes sense (when you think of option as a failure/success representation) that Option.of_enum (Enum.filter ...) behaves roughly as an Enum.find. > If we have to write a bind, I'd keep it consistent with other > OCaml-based binds, i.e. in the same order as Haskell. And it's the order that pa_monad use. Agreed. Below is a new patch with the usual parameters order. Against option.mli : 32a33,35 > val bind : 'a option -> ('a -> 'b option) -> 'b option > (** [bind None f] returns [None] and [bind (Some x) f] returns [f x] *) > Against option.ml : 26a27,30 > let bind opt f = match opt with > | None -> None > | Some v -> f v > Quick usage test : # let may_sum a b = Option.bind a (fun a_val -> Option.bind b (fun b_val -> Some (a_val + b_val)));; val may_sum : int option -> int option -> int option = <fun> # may_sum (Some 1) (Some 2), may_sum (Some 2) None;; - : int option * int option = (Some 3, None) |