From 2e607daf67c9d6f5da2116bcdb1cf3b11add8d00 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Mon, 31 Aug 2015 18:55:12 +0200 Subject: [PATCH] a1 almost done, gitignore --- .gitignore | 2 + a1/mart/skeleton1.icl | 89 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 79 insertions(+), 12 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6d9bc01 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +a.out +Clean System Files diff --git a/a1/mart/skeleton1.icl b/a1/mart/skeleton1.icl index af8a628..3e13262 100644 --- a/a1/mart/skeleton1.icl +++ b/a1/mart/skeleton1.icl @@ -1,10 +1,6 @@ module skeleton1 - -/* - Course I00032 Advanced Programming 2014 - Skeleton for assignment 1 - Pieter Koopman -*/ +//Charlie Gerhardus (s000000) +//Mart Lubbers (s4109503) import StdEnv @@ -15,7 +11,7 @@ import StdEnv :: Rose a = Rose a [Rose a] // Binary sums and products (in generic prelude) -:: UNIT = UNIT +:: UNIT = UNIT :: PAIR a b = PAIR a b :: EITHER a b = LEFT a | RIGHT b @@ -23,12 +19,11 @@ import StdEnv :: RoseG a :== PAIR a [Rose a] // Conversions -fromRose :: (Rose a) -> RoseG a -fromRose (Rose a l) = PAIR a l - -// Oerdering +fromRose :: (Rose a) -> RoseG a +fromRose (Rose a l)= PAIR a l -:: Ordering = Smaller | Equal | Bigger +// Ordering +:: Ordering = Smaller | Equal | Bigger class (><) infix 4 a :: !a !a -> Ordering @@ -56,3 +51,73 @@ instance >< Bool where // False is smaller than True (><) _ _ = Equal /**************** End Prelude *************************/ +// 1. Ordering by overloading +instance >< Color where + (><) Red Red = Equal + (><) Red _ = Bigger + (><) Yellow Yellow = Equal + (><) Yellow Red = Smaller + (><) Yellow Blue = Bigger + (><) Blue Blue = Equal + (><) _ _ = Smaller + +instance >< (Tree a) | >< a & == a where + (><) Tip Tip = Equal + (><) (Bin _ _ _) Tip = Bigger + (><) Tip (Bin _ _ _) = Smaller + (><) (Bin x ltx rtx) (Bin y lty rty) + | x == y = case ltx >< lty of + Equal = rtx >< rty + otherwise = ltx >< lty + | otherwise = x >< y + +instance >< (Rose a) | >< a & == a where + (><) (Rose _ _) (Rose _ []) = Bigger + (><) (Rose _ []) (Rose _ _) = Smaller + (><) (Rose x xs) (Rose y ys) + | x == y = xs >< ys + | otherwise = x >< y + +instance >< [a] | Ord a & == a where + (><) [] [] = Equal + (><) [] _ = Smaller + (><) _ [] = Bigger + (><) [x:xs] [y:ys] + | x == y = xs >< ys + | x < y = Smaller + | otherwise = Bigger + +instance >< (a, b) | >< a & >< b & == a where + (><) (xa, xb) (ya, yb) + | xa == ya = xb >< yb + | otherwise = xa >< ya + +//2. Generic representation +//2.1 +:: ColorG :== EITHER UNIT (EITHER UNIT UNIT) +:: ListG a :== EITHER UNIT (PAIR a [a]) + +//2.2 +listToGen :: [a] -> ListG a +listToGen [] = LEFT UNIT +listToGen [x:xs] = RIGHT (PAIR x xs) + +//2.3. EITHER (PAIR 1 (PAIR 2 3)) UNIT +// Nope, it will leave the xs to be so it will be: EITHER (PAIR 1 [2,3]) +//2.4. Nope, not possible since they are basic types and they can't be +// converted to UNIT, EITHER or PAIR. + +//3. Ordering via generic representation +instance >< UNIT where + (><) _ _ = Equal + +instance >< (PAIR a b) | >< a & >< b + (><) (PAIR x1 y1) (PAIR x2 y2) = case x1 >< x2 of + Equal = y1 >< y2 + otherwise = x1 >< x2 + +instance >< (EITHER a b) | >< a & >< b + (><) (LEFT _) (RIGHT _) = Bigger + (><) (RIGHT _) (LEFT _) = Smaller + (><) (RIGHT x) (RIGHT y) = x >< y + (><) (LEFT x) (LEFT y) = x >< y -- 2.20.1