Exercise : Exceptions
- Write a function call with type ∀αβ. (α → β) → α → β such that
call f argreturns the result offapplied toargand prints any exception that was raised byf. The exception must be re-raised again, once printed.
To print an exception, you need the functionPrintexc.to_stringsee the module Printexc in the standard library. Note thatcallalready exists in module Printexc, it is namedprint. - Test it. Use
test_raise - Define a variant type:
type 'a result = Ok of 'a | Error of exn - Write a function eval with type ∀αβ. (α → β) → α → β result such that
eval f argreturnsOk xiff argreturns x, orError exif the exception ex was raised. - Write a function check_all with type ∀αβ. (α → β) → (α * β result) list → bool such that
check_all f test_listreturns true iff f returns the expected result for all the tests of the list.check_all take_two []true check_all take_two [ ([1], Error Not_found) ]true check_all take_two [ ([1], Ok (1,1)) ]false check_all take_two [ ([1], Error Not_found) ; ([4;3;2;1], Ok (4,3)) ]true check_all take_two [ ([1], Error Not_found) ; ([4;3;2;1], Error Not_found) ]false
(This is how your functions will be evaluated during the exam.)