Jane Street OCaml Challenge: Mutable Records

Records with the mutable keyword allow updating fields in place using <-.

Color and Stoplight Types

type color = | Red | Yellow | Green [@@deriving compare] type stoplight = { location : string ; mutable color : color } [@@deriving compare]

Creating and Updating

let an_example : stoplight = { location = "The corner of Vesey Street and the West Side highway"; color = Red } let set_color stoplight color = stoplight.color <- color

Your Turn: Advance the Color

Advance color in-place: Red → Green, Green → Yellow, Yellow → Red.

let advance_color stoplight = failwith "For you to implement"

Tests

module For_testing = struct let test_ex_red : stoplight = { location = ""; color = Red } let test_ex_red' : stoplight = { test_ex_red with color = Green } let test_ex_yellow : stoplight = { location = ""; color = Yellow } let test_ex_yellow' : stoplight = { test_ex_yellow with color = Red } let test_ex_green : stoplight = { location = ""; color = Green } let test_ex_green' : stoplight = { test_ex_green with color = Yellow } let%test "Testing advance_color..." = advance_color test_ex_green; [%compare.equal: stoplight] test_ex_green' test_ex_green let%test "Testing advance_color..." = advance_color test_ex_yellow; [%compare.equal: stoplight] test_ex_yellow' test_ex_yellow let%test "Testing advance_color..." = advance_color test_ex_red; [%compare.equal: stoplight] test_ex_red' test_ex_red end