From: Mart Lubbers Date: Mon, 23 Feb 2015 15:28:42 +0000 (+0100) Subject: began with week 3, 6.5 X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=7199ded1d33e3d5541344cfd46472e561af491a5;p=fp1415.git began with week 3, 6.5 --- diff --git a/week3/mart/StdSortList.dcl b/week3/mart/StdSortList.dcl new file mode 100644 index 0000000..46bd238 --- /dev/null +++ b/week3/mart/StdSortList.dcl @@ -0,0 +1,18 @@ +definition module StdSortList + +import StdClass + +:: SortList a + +newSortList :: SortList 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 +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 diff --git a/week3/mart/StdSortList.icl b/week3/mart/StdSortList.icl new file mode 100644 index 0000000..2c8ad3f --- /dev/null +++ b/week3/mart/StdSortList.icl @@ -0,0 +1,43 @@ +implementation module StdSortList + +import StdEnv + +:: SortList a = SortList (SortList a, a, SortList a) | Empty + +newSortList :: SortList a +newSortList = Empty + +memberSort :: a (SortList a) -> Bool | Eq, Ord a +memberSort x Empty = False +memberSort x (le, el, gr) +| x == e = True +| x < el = memberSort x le +| otherwise = memberSort x gr + +insertSort :: a (SortList a) -> SortList a | Ord a +memberSort x Empty = Sortlist (Empty, x, Empty) +memberSort x (le, el, gr) + +removeFirst :: a (SortList a) -> SortList a | Eq, Ord a +removeFirst e ([], _)_ = ([], _) + +removeAll :: a (SortList a) -> SortList a +removeAll _ _ = Empty + +elements :: (SortList a) -> [a] +elements _ = [] + +count :: (SortList a) -> Int +count _ = 0 + +minimum :: (SortList a) -> a +minimum _ = 0 + +maximum :: (SortList a) -> a +maximum _ = 0 + +mergeSortList :: (SortList a) (SortList b) -> (SortList a) +mergeSortList _ _ = Empty + +Start :: String +Start = newSortList diff --git a/week3/mart/StdStack.dcl b/week3/mart/StdStack.dcl new file mode 100644 index 0000000..8c861a1 --- /dev/null +++ b/week3/mart/StdStack.dcl @@ -0,0 +1,13 @@ +definition module StdStack + +:: Stack a + +newStack :: Stack a // lege stack +push :: a (Stack a) -> Stack a // plaats nieuw element bovenop de stack +pushes :: [a] (Stack a) -> Stack a // plaats elementen achtereenvolgens bovenop stack +pop :: (Stack a) -> Stack a // haal top element van stack +popn :: Int (Stack a) -> Stack a // haal bovenste $n$ top elementen van stack +top :: (Stack a) -> a // geef top element van stack +topn :: Int (Stack a) -> [a] // geef bovenste $n$ top elementen van stack +elements :: (Stack a) -> [a] // geef alle elementen van stack +count :: (Stack a) -> Int // tel aantal elementen in stack diff --git a/week3/mart/StdStack.icl b/week3/mart/StdStack.icl new file mode 100644 index 0000000..13220e1 --- /dev/null +++ b/week3/mart/StdStack.icl @@ -0,0 +1,57 @@ +implementation module StdStack + +import StdEnv + +:: Stack a = Stack [a] + +newStack :: Stack a +newStack = Stack [] + +push :: a (Stack a) -> Stack a +push x (Stack xs) = Stack [x:xs] + +pushes :: [a] (Stack a) -> Stack a +pushes [] (Stack s) = Stack s +pushes [x:xs] (Stack s) = pushes xs (push x (Stack s)) + +pop :: (Stack a) -> Stack a +pop (Stack []) = abort "Can't pop from empty stack..." +pop (Stack [x:xs]) = Stack xs + +popn :: Int (Stack a) -> Stack a +popn 0 s = s +popn n s = popn (n-1) (pop s) + +top :: (Stack a) -> a +top (Stack []) = abort "Can't give top of empty stack..." +top (Stack [x:_]) = x + +topn :: Int (Stack a) -> [a] +topn 0 _ = [] +topn n x = [top x:topn (n-1) (pop x)] + +elements :: (Stack a) -> [a] +elements (Stack s) = s + +count :: (Stack a) -> Int +count (Stack []) = 0 +count (Stack [_:xs]) = 1 + count (Stack xs) + +Start = ( "s0 = newStack = ", s0,'\n' + , "s1 = push 1 s0 = ", s1,'\n' + , "s2 = pushes [2..5] s1 = ",s2,'\n' + , "s3 = pop s2 = ", s3,'\n' + , "s4 = popn 3 s3 = ", s4,'\n' + , "s5 = top s4 = ", s5,'\n' + , "s6 = topn 3 s2 = ", s6,'\n' + , "s7 = elements s2 = ", s7,'\n' + ) +where + s0 = newStack + s1 = push 1 s0 + s2 = pushes [2..5] s1 + s3 = pop s2 + s4 = popn 3 s3 + s5 = top s4 + s6 = topn 3 s2 + s7 = elements s2