-
Notifications
You must be signed in to change notification settings - Fork 0
/
midterm.sml
80 lines (68 loc) · 1.32 KB
/
midterm.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
(*
fun mystery_1 f xs =
let
fun g xs =
case xs of
[] => NONE
| x::xs' => if f x then SOME x else g xs'
in
case xs of
[] => NONE
| x::xs' => if f x then g xs' else mystery_1 f xs'
end
fun mystery_2 f = fn xs =>
let
fun g xs =
case xs of
[] => NONE
| x::xs' => if f x then SOME x else g xs'
in
case xs of
[] => NONE
| x::xs' => if f x then g xs' else mystery_2 f xs'
end
*)
(* 11
signature COUNTER =
sig
type t = int
val newCounter : int -> t
val increment : t -> t
val first_larger : t * t -> bool
end
*)
(* 12
signature COUNTER =
sig
type t = int
val newCounter : int -> t
val first_larger : t * t -> bool
end
*)
(* 13
signature COUNTER =
sig
type t
val newCounter : int -> int
val increment : t -> t
val first_larger : t * t -> bool
end
*)
signature COUNTER =
sig
type t
val newCounter : int -> t
val increment : t -> t
val first_larger : t * t -> bool
end
structure NoNegativeCounter :> COUNTER =
struct
exception InvariantViolated
type t = int
fun newCounter i = if i <= 0 then 1 else i
fun increment i = i + 1
fun first_larger (i1,i2) =
if i1 <= 0 orelse i2 <= 0
then raise InvariantViolated
else (i1 - i2) > 0
end