Jane Street OCaml Challenge: List Functions
Using List.fold
Rewrite sum and product using List.fold
:
let simpler_sum xs = failwith "For you to implement"
let simpler_product xs = failwith "For you to implement"
Using List.map
Transform a list of ints into a list of floats:
let float_of_int xs = failwith "For you to implement"
Using List.init
Rewrite the range
function using List.init
:
let range from to_ = failwith "For you to implement"
Using List.iter
Print each int in a list:
let print_int_list xs = failwith "For you to implement"
Other Useful Functions (Just for reference)
List.find
: returns the first match as an option
List.filter
: keeps elements matching a condition
List.mapi
: like map
but includes indices
List.zip
: pairs elements from two lists
Tests
let%test "Testing simpler_product..." = Int.( = ) 1 (simpler_product [])
let%test "Testing simpler_product..." = Int.( = ) 55 (simpler_product [ 55 ])
let%test "Testing simpler_product..." = Int.( = ) 25 (simpler_product [ 5; -5; 1; -1 ])
let%test "Testing simpler_product..." = Int.( = ) 25 (simpler_product [ 5; 5; 1; 1 ])
let%test "Testing simpler_sum..." = Int.( = ) 0 (simpler_sum [])
let%test "Testing simpler_sum..." = Int.( = ) 55 (simpler_sum [ 55 ])
let%test "Testing simpler_sum..." = Int.( = ) 0 (simpler_sum [ 5; -5; 1; -1 ])
let%test "Testing simpler_sum..." = Int.( = ) 12 (simpler_sum [ 5; 5; 1; 1 ])
let%test "Testing float_of_int..." = [%compare.equal: float list] (float_of_int [1; 2; 3]) [ 1.0; 2.0; 3.0 ]
let%test "Testing range..." = [%compare.equal: int list] (range 1 4) [ 1; 2; 3 ]
let%test "Testing range..." =
[%compare.equal: int list] (range (-5) 3) [ -5; -4; -3; -2; -1; 0; 1; 2 ]