Added student numbers
[fp1415.git] / fp1 / week2 / mart / StdT.icl
1 implementation module StdT
2
3 import StdEnv
4
5 :: T = {m :: Int, s :: Int}
6
7 instance == T where
8 == a b = a.m == b.m && a.s == b.s
9 instance < T where
10 < a b = a.m < b.m || a.s == b.s && a.s < b.s
11
12 instance zero T where
13 zero = {m=zero, s=zero}
14 instance + T where
15 + a b = fromInt (toInt a + toInt b)
16 instance - T where
17 - a b = fromInt (toInt a - toInt b)
18
19 instance toInt T where
20 toInt a = a.m*60 + a.s
21 instance fromInt T where
22 fromInt a
23 | a<0 = zero
24 | otherwise = {m=a/60, s=a rem 60}
25
26 instance toString T where
27 toString {m=ms, s=0} = toString ms +++ ":00"
28 toString {m=ms, s=ss}
29 | ss < 10 = toString ms +++ ":0" +++ toString ss
30 | otherwise = toString ms +++ ":" +++ toString ss
31
32 instance fromString T where
33 fromString a
34 | a.[size a - 3] == ':' = {m = toInt (a % (0, (size a) - 4)), s = toInt (a % ((size a) - 2, size a))}
35 | otherwise = zero