From 6f604b19d3f5966e5c1d7c4fdf3703bd6ff0861c Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Thu, 16 Apr 2015 21:22:20 +0200 Subject: update to fp2 yay, public and licence --- fp1/week2/mart/StdT.icl | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 fp1/week2/mart/StdT.icl (limited to 'fp1/week2/mart/StdT.icl') diff --git a/fp1/week2/mart/StdT.icl b/fp1/week2/mart/StdT.icl new file mode 100644 index 0000000..01bee7d --- /dev/null +++ b/fp1/week2/mart/StdT.icl @@ -0,0 +1,35 @@ +implementation module StdT + +import StdEnv + +:: T = {m :: Int, s :: Int} + +instance == T where + == a b = a.m == b.m && a.s == b.s +instance < T where + < a b = a.m < b.m || a.s == b.s && a.s < b.s + +instance zero T where + zero = {m=zero, s=zero} +instance + T where + + a b = fromInt (toInt a + toInt b) +instance - T where + - a b = fromInt (toInt a - toInt b) + +instance toInt T where + toInt a = a.m*60 + a.s +instance fromInt T where + fromInt a + | a<0 = zero + | otherwise = {m=a/60, s=a rem 60} + +instance toString T where + toString {m=ms, s=0} = toString ms +++ ":00" + toString {m=ms, s=ss} + | ss < 10 = toString ms +++ ":0" +++ toString ss + | otherwise = toString ms +++ ":" +++ toString ss + +instance fromString T where + fromString a + | a.[size a - 3] == ':' = {m = toInt (a % (0, (size a) - 4)), s = toInt (a % ((size a) - 2, size a))} + | otherwise = zero -- cgit v1.2.3