March 2019
Intermediate to advanced
208 pages
5h 11m
English
Consider this short module that defines a “time of day” type as a record with fields for the hour and minute:
| | /* Interface */ |
| | module type Time { |
| | type t = { |
| | hour: int, |
| | minute:int |
| | }; |
| | |
| | let make: (int, int) => t; |
| | let add: (t, t) => t; |
| | }; |
| | |
| | /* Implementation */ |
| | module Time: Time = { |
| | type t = { |
| | hour: int, |
| | minute:int |
| | }; |
| | |
| | let make = (h:int, m:int): t => { |
| | {hour: abs(h) mod 24, |
| | minute: abs(m) mod 60} |
| | } |
| | |
| | let add = (t1: t, t2: t): t => { |
| | let total = (t1.hour * 60 + t1.minute) + |
| | (t2.hour * 60 + t2.minute); |
| | make(total / 60, ... |
Read now
Unlock full access