update to fp2 yay, public and licence
[fp1415.git] / fp1 / week2 / camil / StdT.icl
1 /**
2 * Mart Lubbers, s4109503
3 * Camil Staps, s4498062
4 */
5
6 implementation module StdT
7
8 import StdEnv
9
10 :: T = {m :: Int, s :: Int}
11
12 instance == T where == a b = a.m == b.m && a.s == b.s
13 instance < T where < a b = a.m < b.m || a.m == b.m && a.s < b.s
14
15 instance zero T where zero = {m = zero, s = zero}
16 instance + T where + a b = fromInt (toInt a + toInt b)
17 instance - T where - a b = if (a < b) zero (fromInt (toInt a - toInt b))
18
19 instance toInt T where toInt a = a.m * 60 + a.s
20 instance fromInt T where fromInt n = if (n < 0) zero {m = n/60, s = n rem 60}
21
22 instance toString T where
23 toString {m = x, s = 0} = toString x +++ ":00"
24 toString a = toString a.m +++ ":" +++ (if (a.s < 10) "0" "") +++ toString a.s
25 instance fromString T where
26 fromString s = if (s.[size s - 3] == ':')
27 {m = toInt (s % (0, size s - 4)), s = toInt (s % (size s - 2, size s - 1))}
28 zero
29
30 Start :: (Bool, Bool, T, T, T, Int, String, T, T)
31 Start = (LOTR == Tea, Tea < LOTR,
32 zero + LOTR, LOTR + Tea, Tea - LOTR,
33 toInt LOTR, toString Tea,
34 fromString "5:40", fromString "foo")
35
36 LOTR = {m=178, s=0}
37 Tea = {m=0,s=41}