From: Camil Staps Date: Sat, 28 Feb 2015 13:28:03 +0000 (+0100) Subject: Updated w3 to let maximum have O(1) X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=b7abd2be82acb79fce943a30a2d5078e01218d67;p=fp1415.git Updated w3 to let maximum have O(1) --- diff --git a/week3/camil/StdSortList.dcl b/week3/camil/StdSortList.dcl index 46bd238..556dfc0 100644 --- a/week3/camil/StdSortList.dcl +++ b/week3/camil/StdSortList.dcl @@ -4,15 +4,15 @@ import StdClass :: SortList a -newSortList :: SortList a // lege gesorteerde lijst +newSortList :: SortList a | zero a // lege gesorteerde lijst memberSort :: a (SortList a) -> Bool | Eq, Ord a // is element van insertSort :: a (SortList a) -> SortList a | Ord a // voeg element toe -removeFirst :: a (SortList a) -> SortList a | Eq, Ord a // verwijder eerste voorkomen -removeAll :: a (SortList a) -> SortList a | Eq, Ord a // verwijder alle voorkomens +removeFirst :: a (SortList a) -> SortList a | Eq, Ord, zero a // verwijder eerste voorkomen +removeAll :: a (SortList a) -> SortList a | Eq, Ord, zero a // verwijder alle voorkomens elements :: (SortList a) -> [a] // geef alle elementen count :: (SortList a) -> Int // aantal elementen minimum :: (SortList a) -> a // huidige minimum waarde maximum :: (SortList a) -> a // huidige maximum waarde -mergeSortList :: (SortList a) (SortList a) -> SortList a | Eq, Ord a // meng gesorteerde lijsten +mergeSortList :: (SortList a) (SortList a) -> SortList a | Eq, Ord, zero a // meng gesorteerde lijsten diff --git a/week3/camil/StdSortList.icl b/week3/camil/StdSortList.icl index 759e915..21778bd 100644 --- a/week3/camil/StdSortList.icl +++ b/week3/camil/StdSortList.icl @@ -1,60 +1,64 @@ +// Ik kreeg het alleen werkend door de .dcl ook aan te passen. +// Met een record dat het maximum bijhoudt moet je er namelijk vanuit kunnen gaan dat zero gedefinieerd is voor type a. + implementation module StdSortList import StdEnv -:: SortList a :== [a] +:: SortList a = {list :: [a], max :: a} -newSortList :: (SortList a) -newSortList = [] +newSortList :: (SortList a) | zero a +newSortList = {list=[], max=zero} memberSort :: a (SortList a) -> Bool | Eq, Ord a // is element van -memberSort _ [] = False +memberSort _ {list=[],max=_} = False memberSort m l - | hd l == m = True - | hd l > m = False - | otherwise = memberSort m (tl l) + | minimum l == m = True + | minimum l > m = False + | otherwise = memberSort m {list=(tl l.list),max=l.max} insertSort :: a (SortList a) -> SortList a | Ord a // voeg element toe -insertSort m l = insertSort` newSortList m l +insertSort m l = insertSort` {list=[],max=l.max} m l where insertSort` :: (SortList a) a (SortList a) -> (SortList a) | Ord a insertSort` l1 m l2 - | count l2 == 0 = l1 ++ [m] - | minimum l2 >= m = l1 ++ [m] ++ l2 - | otherwise = insertSort` (l1 ++ [hd l2]) m (tl l2) + | count l2 == 0 = {list=l1.list ++ [m], max=m} + | minimum l2 >= m = {list=l1.list ++ [m] ++ l2.list, max=l2.max} + | otherwise = insertSort` {list=l1.list ++ [hd l2.list], max=hd l2.list} m {list=(tl l2.list), max=l2.max} -removeFirst :: a (SortList a) -> SortList a | Eq, Ord a // verwijder eerste voorkomen +removeFirst :: a (SortList a) -> SortList a | Eq, Ord, zero a // verwijder eerste voorkomen removeFirst m l = removeFirst` newSortList m l where removeFirst` :: (SortList a) a (SortList a) -> (SortList a) | Eq, Ord a removeFirst` l1 m l2 | count l2 == 0 = l1 - | minimum l2 > m = l1 ++ l2 - | minimum l2 == m = l1 ++ tl l2 - | otherwise = removeFirst` (l1 ++ [hd l2]) m (tl l2) + | minimum l2 > m = {list=l1.list ++ l2.list, max=l2.max} + | minimum l2 == m && count l2 == 1 = {list=l1.list ++ tl l2.list, max=l1.max} + | minimum l2 == m = {list=l1.list ++ tl l2.list, max=l2.max} + | otherwise = removeFirst` {list=(l1.list ++ [hd l2.list]),max=hd l2.list} m {list=(tl l2.list), max=l2.max} -removeAll :: a (SortList a) -> SortList a | Eq, Ord a // verwijder alle voorkomens +removeAll :: a (SortList a) -> SortList a | Eq, Ord, zero a // verwijder alle voorkomens removeAll m l = removeAll` newSortList m l where removeAll` :: (SortList a) a (SortList a) -> (SortList a) | Eq, Ord a removeAll` l1 m l2 | count l2 == 0 = l1 - | minimum l2 > m = l1 ++ l2 - | minimum l2 == m = removeAll` l1 m (tl l2) - | otherwise = removeAll` (l1 ++ [hd l2]) m (tl l2) + | minimum l2 > m = {list=l1.list ++ l2.list, max=l2.max} + | minimum l2 == m = removeAll` l1 m {list=tl l2.list, max=l2.max} + | otherwise = removeAll` {list=l1.list ++ [hd l2.list], max=hd l2.list} m {list=tl l2.list,max=l2.max} elements :: (SortList a) -> [a] // geef alle elementen -elements l = l +elements l = l.list count :: (SortList a) -> Int // aantal elementen -count l = length l +count l = length l.list minimum :: (SortList a) -> a // huidige minimum waarde -minimum l = hd l +minimum l = hd l.list maximum :: (SortList a) -> a // huidige maximum waarde -maximum l = last l +maximum l = l.max -mergeSortList :: (SortList a) (SortList a) -> SortList a | Eq, Ord a // meng gesorteerde lijsten -mergeSortList l1 [] = l1 -mergeSortList l1 l2 = mergeSortList (insertSort (hd l2) l1) (tl l2) +mergeSortList :: (SortList a) (SortList a) -> SortList a | Eq, Ord, zero a // meng gesorteerde lijsten +mergeSortList l1 {list=[],max=_} = l1 +mergeSortList l1 l2 = mergeSortList (insertSort (hd l2.list) l1) {list=tl l2.list,max=l2.max}