ocaml: phase 5.1 exception_user.ml baseline (user exception with payload, 4+5+7+10 = 26)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 47s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 47s
Defines a user exception with int payload:
exception Negative of int
let safe_sqrt n =
if n < 0 then raise (Negative n)
else <integer sqrt via while loop>
let try_sqrt n =
try safe_sqrt n with
| Negative x -> -x
try_sqrt 16 -> 4
try_sqrt 25 -> 5
try_sqrt -7 -> 7 (handler returns -(-7) = 7)
try_sqrt 100 -> 10
sum -> 26
Tests exception declaration with int payload, raise with carry, and
try-with arm pattern-matching the constructor with payload binding.
56 baseline programs total.
This commit is contained in:
16
lib/ocaml/baseline/exception_user.ml
Normal file
16
lib/ocaml/baseline/exception_user.ml
Normal file
@@ -0,0 +1,16 @@
|
||||
exception Negative of int
|
||||
|
||||
let safe_sqrt n =
|
||||
if n < 0 then raise (Negative n)
|
||||
else
|
||||
let g = ref 1 in
|
||||
while !g * !g < n do g := !g + 1 done;
|
||||
!g
|
||||
|
||||
let try_sqrt n =
|
||||
try safe_sqrt n with
|
||||
| Negative x -> -x
|
||||
|
||||
;;
|
||||
|
||||
try_sqrt 16 + try_sqrt 25 + try_sqrt (-7) + try_sqrt 100
|
||||
Reference in New Issue
Block a user