Jane Street OCaml Challenge: Average Function
Here are some example functions that you’ll reuse:
let square x = x * x
let half x = x / 2
let add x y = x + y
Function Invocation Styles
You can order function calls using parentheses:
let () =
Stdio.printf "(5^2)/2 = %i" (half (square 5))
Or using intermediate let-bindings:
let () =
let squared = square 5 in
let halved = half squared in
Stdio.printf "(5^2)/2 = %i" halved
Write Your Own Function
Now try writing an average
function by combining add
and half
.
let average x y = failwith "For you to implement"
Tests
let%test "Testing average..." = Int.(=) 5 (average 5 5)
let%test "Testing average..." = Int.(=) 75 (average 50 100)