| 1 | <h2>Comments</h2> |
| 2 | <pre><code>(* Simple comment *) |
| 3 | (* Multi-line |
| 4 | comment *)</code></pre> |
| 5 | |
| 6 | <h2>Numbers</h2> |
| 7 | <pre><code>42 |
| 8 | 3.14159 |
| 9 | 42. |
| 10 | 2.4E+2 |
| 11 | 10_452_102 |
| 12 | 0xf4 |
| 13 | 0xff_10_41 |
| 14 | 0o427 |
| 15 | 0b1100_1111_0000</code></pre> |
| 16 | |
| 17 | <h2>Strings and characters</h2> |
| 18 | <pre><code>"Simple string." |
| 19 | "String with \"quotes\" in it." |
| 20 | 'c' `c` |
| 21 | '\'' `\`` |
| 22 | '\123' `\123` |
| 23 | '\xf4'</code></pre> |
| 24 | |
| 25 | <h2>Full example</h2> |
| 26 | <pre><code>module Make_interval(Endpoint : Comparable) = struct |
| 27 | |
| 28 | type t = | Interval of Endpoint.t * Endpoint.t |
| 29 | | Empty |
| 30 | |
| 31 | (** [create low high] creates a new interval from [low] to |
| 32 | [high]. If [low > high], then the interval is empty *) |
| 33 | let create ~low ~high = |
| 34 | if Endpoint.compare low high > 0 then Empty |
| 35 | else Interval (low,high) |
| 36 | |
| 37 | (** Returns true iff the interval is empty *) |
| 38 | let is_empty = function |
| 39 | | Empty -> true |
| 40 | | Interval _ -> false |
| 41 | |
| 42 | (** [contains t x] returns true iff [x] is contained in the |
| 43 | interval [t] *) |
| 44 | let contains t x = |
| 45 | match t with |
| 46 | | Empty -> false |
| 47 | | Interval (l,h) -> |
| 48 | Endpoint.compare x l >= 0 && Endpoint.compare x h <= 0 |
| 49 | |
| 50 | (** [intersect t1 t2] returns the intersection of the two input |
| 51 | intervals *) |
| 52 | let intersect t1 t2 = |
| 53 | let min x y = if Endpoint.compare x y <= 0 then x else y in |
| 54 | let max x y = if Endpoint.compare x y >= 0 then x else y in |
| 55 | match t1,t2 with |
| 56 | | Empty, _ | _, Empty -> Empty |
| 57 | | Interval (l1,h1), Interval (l2,h2) -> |
| 58 | create ~low:(max l1 l2) ~high:(min h1 h2) |
| 59 | |
| 60 | end ;;</code></pre> |