From: Camil Staps Date: Sun, 22 Feb 2015 21:54:02 +0000 (+0100) Subject: 6.5 working X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=56a5e0f968fc99cd1dc4731b0d1cc572f07d6f89;p=fp1415.git 6.5 working --- diff --git a/week3/camil/.gitignore b/week3/camil/.gitignore index 3d25840..341d5f8 100644 --- a/week3/camil/.gitignore +++ b/week3/camil/.gitignore @@ -1 +1,2 @@ /Clean System Files/ +StdStack diff --git a/week3/camil/StdStack.icl b/week3/camil/StdStack.icl index 25ff265..dd51a94 100644 --- a/week3/camil/StdStack.icl +++ b/week3/camil/StdStack.icl @@ -1,6 +1,7 @@ implementation module StdStack import StdEnv +import StdList :: Stack a :== [a] @@ -8,9 +9,34 @@ newStack :: (Stack a) newStack = [] push :: a (Stack a) -> (Stack a) -push a s = s ++ [a] +push a s = [a] ++ s -pop +pop :: (Stack a) -> (Stack a) +pop [a:s] = s +pop [] = [] + +popn :: Int (Stack a) -> (Stack a) +popn 0 s = s +popn n s = popn (n-1) (pop s) + +pushes :: [a] (Stack a) -> (Stack a) +pushes [] s = s +pushes a s = pushes (tl a) (push (hd a) s) + +top :: (Stack a) -> a +top [] = abort "`top s` with s = []" +top s = hd s + +topn :: Int (Stack a) -> [a] +topn n s + | n > length s = abort "`topn n s` with n > length s" + | otherwise = take n s + +count :: (Stack a) -> Int +count s = length s + +elements :: (Stack a) -> [a] +elements s = s Start = ( "s0 = newStack = ", s0,'\n' , "s1 = push 1 s0 = ", s1,'\n' @@ -20,6 +46,10 @@ Start = ( "s0 = newStack = ", s0,'\n' , "s5 = top s4 = ", s5,'\n' , "s6 = topn 3 s2 = ", s6,'\n' , "s7 = elements s2 = ", s7,'\n' +// , "s8 = push 10 s1 = ", s8,'\n' +// , "s9 = popn 10 s8 = ", s9,'\n' +// , "sa = topn 5 s4 = ", sa,'\n' +// , "sb = top s0 = ", sb,'\n' ) where s0 = newStack @@ -30,3 +60,7 @@ where s5 = top s4 s6 = topn 3 s2 s7 = elements s2 +// s8 = push 10 s1 +// s9 = popn 10 s8 +// sa = topn 5 s4 +// sb = top s0