From: Charlie Gerhardus Date: Sun, 6 Sep 2015 19:24:09 +0000 (+0200) Subject: final versie X-Git-Tag: assignment2~5^2 X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=e272644133b4f69e308b38ca4036cd325e3af828;p=ap2015.git final versie --- diff --git a/a1/charlie/skeleton1.icl b/a1/charlie/skeleton1.icl index 3a5e8d4..0959e15 100755 --- a/a1/charlie/skeleton1.icl +++ b/a1/charlie/skeleton1.icl @@ -1,9 +1,44 @@ /* assignment 1 * * Charlie Gerhardus, s3050009 - * Mart Lubbers s4109503 + * Mart Lubbers, s4109503 */ +/* awnsers to questions + * + * 2.3, [1, 2, 3] has the full generic representation PAIR 1 (PAIR 2 3) + * the >-< operator converts the second PAIR on the fly so the initial generic representation + * returned by fromList [1, 2, 3] = PAIR 1 [2, 3]. + * + * 2.4, Its possible to define a class for a generic conversion function: + * + * class toGen a b :: a -> b + * + * For every type we want to convert we need to create a instance: + * + * instance toGen [a] (ListG a) where + * toGen x = fromList x + * instance toGen (Tree a) (TreeG a) where + * toGen x = fromTree x + * .... + * + * Which results in the same code as can be seen later in the assignment, the difference + * is that instead of a multitude of different functions (fromList, fromTree...) we end up + * with a lot of overloaded functions. + * + * 3.2, yes the results are identical. This can be verified by compile and running this module. + * + * 3.3, when we define a new datastructure and provide a function that converts it into a generic + * representation we can use our ordering operator on it right away. If we define more operators + * for our generic representation we dont have to provide a sepperate implementation of these operators for + * every datastructure we introduce. + * + * 3.4, since we are rewriting the whole datastructure before we can use the operator the programm can + * potentionally use more memory and result in more instructions that take care of the conversion. + * + */ + + module skeleton1 /* @@ -13,6 +48,7 @@ module skeleton1 */ import StdEnv +import StdFile /**************** Prelude: *******************************/ // Example types @@ -203,11 +239,58 @@ rose1 = Rose 2 [ Rose 1 [], Rose 2 [], Rose 5 [] ] rose2 :: Rose Int rose2 = Rose 2 [ Rose 1 [], Rose 2 [], Rose 8 [] ] -/* our two comparison lists */ -cmp1 :: [Ordering] -cmp1 = [[1..3] >< [1..2], [1..2] >< [1..5], (1,2) >< (1,2), (1,3) >-< (1,2), Red >< Yellow, Yellow >< Blue, tree1 >< tree1, tree1 >< tree2, tree2 >< tree1, rose1 >< rose1, rose1 >< rose2, rose2 >< rose1] -cmp2 :: [Ordering] -cmp2 = [[1..3] >-< [1..2], [1..2] >-< [1..5], (1,2) >-< (1,2), (1,3) >-< (1,2), Red >-< Yellow, Yellow >-< Blue, tree1 >-< tree1, tree1 >-< tree2, tree2 >-< tree1, rose1 >-< rose1, rose1 >-< rose2, rose2 >-< rose1] +/* apply a ordering operator on two values */ +order :: (a a -> Ordering) (a, a) -> Ordering +order f (x, y) = f x y + +/* order a list */ +orderList :: (a a -> Ordering) [ (a, a) ] -> [Ordering] +orderList f [] = [] +orderList f [x:xs] = [ order f x : orderList f xs ] + +/* tests to perform */ +orderSetLists = [([1..3], [1..2]), ([1..2], [1..5]), ([1..2], [1..2])] +orderSetTuples = [((1,3), (1,2)), ((1,2), (1,3)), ((1,2),(1,2))] +orderSetColors = [(Yellow, Blue), (Blue, Yellow), (Red, Red)] +orderSetRoses = [(rose2, rose1), (rose1, rose2), (rose1, rose1)] +orderSetTrees = [(tree2, tree1), (tree1, tree2), (tree1, tree1)] + +/* perform orderings */ +test1 = orderList (><) orderSetLists + ++ orderList (><) orderSetTuples + ++ orderList (><) orderSetLists + ++ orderList (><) orderSetRoses + ++ orderList (><) orderSetTrees +test2 = orderList (>-<) orderSetLists + ++ orderList (>-<) orderSetTuples + ++ orderList (>-<) orderSetLists + ++ orderList (>-<) orderSetRoses + ++ orderList (>-<) orderSetTrees + +/* ordering file output */ +instance <<< Ordering where + (<<<) file Equal = file <<< "Equal" + (<<<) file Bigger = file <<< "Bigger" + (<<<) file Smaller = file <<< "Smaller" + +/* ordering list file output */ +instance <<< [Ordering] where + (<<<) file [] = file + (<<<) file [x:xs] = file <<< x <<< " " <<< xs + +/* there is no file <<< Bool instance? */ +instance <<< Bool where + (<<<) file True = file <<< "True" + (<<<) file False = file <<< "False" /* entry point */ -Start = ([cmp1, cmp2], cmp1 == cmp2) +Start :: !*World -> *World +Start world + # (file, world) = stdio world + # file = file <<< "Test results for ><:\n" + # file = file <<< "[" <<< test1 <<< "]\n\n" + # file = file <<< "Test results for >-<:\n" + # file = file <<< "[" <<< test2 <<< "]\n\n" + # file = file <<< "(test1 == test2) = " <<< (test1 == test2) <<< "\n\n" + # (ok, world) = fclose file world + | otherwise = world