diff --git a/ocaml/out/samples b/ocaml/out/samples index b8953450..dbf252f3 100755 Binary files a/ocaml/out/samples and b/ocaml/out/samples differ diff --git a/ocaml/out/samples.cmi b/ocaml/out/samples.cmi index df5b7e9d..82788a54 100644 Binary files a/ocaml/out/samples.cmi and b/ocaml/out/samples.cmi differ diff --git a/ocaml/out/samples.cmx b/ocaml/out/samples.cmx index e71ce6b1..b2147475 100644 Binary files a/ocaml/out/samples.cmx and b/ocaml/out/samples.cmx differ diff --git a/ocaml/out/samples.o b/ocaml/out/samples.o index 6650a2f3..3c007fac 100644 Binary files a/ocaml/out/samples.o and b/ocaml/out/samples.o differ diff --git a/ocaml/samples.ml b/ocaml/samples.ml index 3da7e583..d35dfd26 100644 --- a/ocaml/samples.ml +++ b/ocaml/samples.ml @@ -18,8 +18,8 @@ let cumsumXs xs = let rec nth xs (n: int) = match xs with - | [] -> None - | y :: ys -> if n = 0 then Some(y) else nth ys (n-1) + | [] -> Error "nth function finds no match" + | y :: ys -> if n = 0 then Ok(y) else nth ys (n-1) (* Note that this is O(n) access. That is the cost of using the nice match syntax, @@ -29,19 +29,19 @@ let rec nth xs (n: int) = let findIndex xs test = let rec recursiveHelper ys i = match ys with - | [] -> None - | z :: zs -> if test z then Some i else recursiveHelper zs (i+1) + | [] -> Error "findIndex doesn't find an index" + | z :: zs -> if test z then Ok(i) else recursiveHelper zs (i+1) in recursiveHelper xs 0 let rec unwind xs = match xs with - | [] -> Some([]) - | None :: ys -> None - | Some(y) :: ys -> ( + | [] -> Ok([]) + | Error e:: ys -> Error e + | Ok(y) :: ys -> ( match unwind ys with - | Some(zs) -> Some(y :: zs) - | None -> None + | Ok(zs) -> Ok(y :: zs) + | Error e -> Error e ) (* Basic samplers *) @@ -64,21 +64,21 @@ let sampleTo low high = let logstd = (loghigh -. loglow) /. (2.0 -. normal_95_ci_length ) in sampleLognormal logmean logstd -let mixture (samplers: (unit -> float) list) (weights: float list): float option = +let mixture (samplers: (unit -> float) list) (weights: float list): (float, string) result = if (List.length samplers == List.length weights) - then None + then Error "in mixture function, List.length samplers != List.length weights" else let normalized_weights = normalizeXs weights in let cumsummed_normalized_weights = cumsumXs normalized_weights in let p = sampleZeroToOne () in let chosenSamplerIndex = findIndex cumsummed_normalized_weights (fun x -> x < p) in let sampler = match chosenSamplerIndex with - | None -> None - | Some(i) -> nth samplers i + | Error e -> Error e + | Ok(i) -> nth samplers i in let sample = match sampler with - | None -> None - | Some(f) -> Some(f ()) + | Error e -> Error e + | Ok(f) -> Ok(f ()) in sample @@ -95,8 +95,8 @@ let () = let n = 1_000_000 in let samples = List.init n (fun _ -> sampler ()) in match unwind samples with - | None -> Printf.printf "error" - | Some(xs) -> ( + | Error err -> Printf.printf "Error %s\n" err + | Ok(xs) -> ( let mean = sumFloats xs /. float_of_int(n) in Printf.printf "Mean: %f" mean )