Questions & Results
What is the value of the OCaml expression?
type point = { x : int; y : int }
let p = { x = 3; y = 4 }
p.x + p.y
What is the type of the OCaml expression?
type student = { name : string; gpa : float }
fun s → s.gpa
What is the value of the OCaml expression?
type point = { x : int; y : int }
let p = { x = 1; y = 2 }
let p2 = { p with y = 5 }
p2.x + p2.y
What is the value of the OCaml expression?
let f x = if x > 0 then Some x else None in
f 5
What is the value of the OCaml expression?
let get_default o =
match o with
| Some x → x
| None → 0
in
get_default None
What is the type of the OCaml expression?
fun v →
match v with
| Some x → x + 1
| None → 0
What is the value of the OCaml expression?
type shape = Circle of float | Square of float
let area s =
match s with
| Circle r → 3.14 *. r *. r
| Square s → s *. s
in
area (Square 4.0)
What is the type of the OCaml expression?
type color = Red | Green | Blue
fun c →
match c with
| Red → 1
| Green → 2
| Blue → 3
What happens when the following OCaml expression is evaluated?
exception Negative
let sqrt_safe x =
if x < 0 then raise Negative else x
in
sqrt_safe (-1)
What is the value of the OCaml expression?
let safe_div x y =
try x / y
with Division_by_zero → -1
in
safe_div 10 0
Score (points) →