\r
:: SortList a\r
\r
-newSortList :: SortList a // lege gesorteerde lijst\r
+newSortList :: SortList a | zero a // lege gesorteerde lijst\r
memberSort :: a (SortList a) -> Bool | Eq, Ord a // is element van\r
insertSort :: a (SortList a) -> SortList a | Ord a // voeg element toe\r
-removeFirst :: a (SortList a) -> SortList a | Eq, Ord a // verwijder eerste voorkomen\r
-removeAll :: a (SortList a) -> SortList a | Eq, Ord a // verwijder alle voorkomens\r
+removeFirst :: a (SortList a) -> SortList a | Eq, Ord, zero a // verwijder eerste voorkomen\r
+removeAll :: a (SortList a) -> SortList a | Eq, Ord, zero a // verwijder alle voorkomens\r
elements :: (SortList a) -> [a] // geef alle elementen\r
count :: (SortList a) -> Int // aantal elementen\r
\r
minimum :: (SortList a) -> a // huidige minimum waarde\r
maximum :: (SortList a) -> a // huidige maximum waarde\r
\r
-mergeSortList :: (SortList a) (SortList a) -> SortList a | Eq, Ord a // meng gesorteerde lijsten\r
+mergeSortList :: (SortList a) (SortList a) -> SortList a | Eq, Ord, zero a // meng gesorteerde lijsten\r
+// Ik kreeg het alleen werkend door de .dcl ook aan te passen. \r
+// Met een record dat het maximum bijhoudt moet je er namelijk vanuit kunnen gaan dat zero gedefinieerd is voor type a.\r
+\r
implementation module StdSortList\r
\r
import StdEnv\r
\r
-:: SortList a :== [a]\r
+:: SortList a = {list :: [a], max :: a}\r
\r
-newSortList :: (SortList a)\r
-newSortList = []\r
+newSortList :: (SortList a) | zero a\r
+newSortList = {list=[], max=zero}\r
\r
memberSort :: a (SortList a) -> Bool | Eq, Ord a // is element van\r
-memberSort _ [] = False\r
+memberSort _ {list=[],max=_} = False\r
memberSort m l\r
- | hd l == m = True\r
- | hd l > m = False\r
- | otherwise = memberSort m (tl l)\r
+ | minimum l == m = True\r
+ | minimum l > m = False\r
+ | otherwise = memberSort m {list=(tl l.list),max=l.max}\r
\r
insertSort :: a (SortList a) -> SortList a | Ord a // voeg element toe\r
-insertSort m l = insertSort` newSortList m l \r
+insertSort m l = insertSort` {list=[],max=l.max} m l \r
where \r
insertSort` :: (SortList a) a (SortList a) -> (SortList a) | Ord a\r
insertSort` l1 m l2\r
- | count l2 == 0 = l1 ++ [m]\r
- | minimum l2 >= m = l1 ++ [m] ++ l2\r
- | otherwise = insertSort` (l1 ++ [hd l2]) m (tl l2)\r
+ | count l2 == 0 = {list=l1.list ++ [m], max=m}\r
+ | minimum l2 >= m = {list=l1.list ++ [m] ++ l2.list, max=l2.max}\r
+ | otherwise = insertSort` {list=l1.list ++ [hd l2.list], max=hd l2.list} m {list=(tl l2.list), max=l2.max}\r
\r
-removeFirst :: a (SortList a) -> SortList a | Eq, Ord a // verwijder eerste voorkomen\r
+removeFirst :: a (SortList a) -> SortList a | Eq, Ord, zero a // verwijder eerste voorkomen\r
removeFirst m l = removeFirst` newSortList m l\r
where\r
removeFirst` :: (SortList a) a (SortList a) -> (SortList a) | Eq, Ord a\r
removeFirst` l1 m l2\r
| count l2 == 0 = l1\r
- | minimum l2 > m = l1 ++ l2\r
- | minimum l2 == m = l1 ++ tl l2\r
- | otherwise = removeFirst` (l1 ++ [hd l2]) m (tl l2)\r
+ | minimum l2 > m = {list=l1.list ++ l2.list, max=l2.max}\r
+ | minimum l2 == m && count l2 == 1 = {list=l1.list ++ tl l2.list, max=l1.max}\r
+ | minimum l2 == m = {list=l1.list ++ tl l2.list, max=l2.max}\r
+ | otherwise = removeFirst` {list=(l1.list ++ [hd l2.list]),max=hd l2.list} m {list=(tl l2.list), max=l2.max}\r
\r
-removeAll :: a (SortList a) -> SortList a | Eq, Ord a // verwijder alle voorkomens\r
+removeAll :: a (SortList a) -> SortList a | Eq, Ord, zero a // verwijder alle voorkomens\r
removeAll m l = removeAll` newSortList m l\r
where\r
removeAll` :: (SortList a) a (SortList a) -> (SortList a) | Eq, Ord a\r
removeAll` l1 m l2\r
| count l2 == 0 = l1\r
- | minimum l2 > m = l1 ++ l2\r
- | minimum l2 == m = removeAll` l1 m (tl l2)\r
- | otherwise = removeAll` (l1 ++ [hd l2]) m (tl l2)\r
+ | minimum l2 > m = {list=l1.list ++ l2.list, max=l2.max}\r
+ | minimum l2 == m = removeAll` l1 m {list=tl l2.list, max=l2.max}\r
+ | otherwise = removeAll` {list=l1.list ++ [hd l2.list], max=hd l2.list} m {list=tl l2.list,max=l2.max}\r
\r
elements :: (SortList a) -> [a] // geef alle elementen\r
-elements l = l\r
+elements l = l.list\r
\r
count :: (SortList a) -> Int // aantal elementen\r
-count l = length l\r
+count l = length l.list\r
\r
minimum :: (SortList a) -> a // huidige minimum waarde\r
-minimum l = hd l\r
+minimum l = hd l.list\r
\r
maximum :: (SortList a) -> a // huidige maximum waarde\r
-maximum l = last l\r
+maximum l = l.max\r
\r
-mergeSortList :: (SortList a) (SortList a) -> SortList a | Eq, Ord a // meng gesorteerde lijsten\r
-mergeSortList l1 [] = l1\r
-mergeSortList l1 l2 = mergeSortList (insertSort (hd l2) l1) (tl l2)\r
+mergeSortList :: (SortList a) (SortList a) -> SortList a | Eq, Ord, zero a // meng gesorteerde lijsten\r
+mergeSortList l1 {list=[],max=_} = l1\r
+mergeSortList l1 l2 = mergeSortList (insertSort (hd l2.list) l1) {list=tl l2.list,max=l2.max}\r