Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
safe_div.ml: integer division returning Result. Sum-safe folds pairs,
skipping the Error branches.
[(10,2); (20,4); (30,0); (50,5)] -> 5 + 5 + 0 + 10 = 20
Result module additions (mirroring real OCaml's signatures):
equal eq_ok eq_err a b
compare cmp_ok cmp_err a b Ok < Error (i.e. Ok x compared to
Error e returns -1)
iter_error f r
Result.equal (=) (=) (Ok 1) (Ok 1) = true
Result.compare compare compare (Ok 5) (Ok 3) = 1
Result.compare compare compare (Ok 1) (Error _) = -1
30 baseline programs total.
15 lines
270 B
OCaml
15 lines
270 B
OCaml
let safe_div a b =
|
|
if b = 0 then Error "division by zero"
|
|
else Ok (a / b)
|
|
|
|
let sum_safe pairs =
|
|
List.fold_left (fun acc (a, b) ->
|
|
match safe_div a b with
|
|
| Ok q -> acc + q
|
|
| Error _ -> acc
|
|
) 0 pairs
|
|
|
|
;;
|
|
|
|
sum_safe [(10, 2); (20, 4); (30, 0); (50, 5)]
|