implementation module StdStack\r
\r
import StdEnv\r
+import StdList\r
\r
:: Stack a :== [a]\r
\r
newStack = []\r
\r
push :: a (Stack a) -> (Stack a)\r
-push a s = s ++ [a]\r
+push a s = [a] ++ s\r
\r
-pop\r
+pop :: (Stack a) -> (Stack a)\r
+pop [a:s] = s\r
+pop [] = []\r
+\r
+popn :: Int (Stack a) -> (Stack a)\r
+popn 0 s = s\r
+popn n s = popn (n-1) (pop s)\r
+\r
+pushes :: [a] (Stack a) -> (Stack a)\r
+pushes [] s = s\r
+pushes a s = pushes (tl a) (push (hd a) s)\r
+\r
+top :: (Stack a) -> a\r
+top [] = abort "`top s` with s = []"\r
+top s = hd s\r
+\r
+topn :: Int (Stack a) -> [a]\r
+topn n s\r
+ | n > length s = abort "`topn n s` with n > length s"\r
+ | otherwise = take n s\r
+\r
+count :: (Stack a) -> Int\r
+count s = length s\r
+\r
+elements :: (Stack a) -> [a]\r
+elements s = s\r
\r
Start = ( "s0 = newStack = ", s0,'\n'\r
, "s1 = push 1 s0 = ", s1,'\n'\r
, "s5 = top s4 = ", s5,'\n'\r
, "s6 = topn 3 s2 = ", s6,'\n'\r
, "s7 = elements s2 = ", s7,'\n'\r
+// , "s8 = push 10 s1 = ", s8,'\n'\r
+// , "s9 = popn 10 s8 = ", s9,'\n'\r
+// , "sa = topn 5 s4 = ", sa,'\n'\r
+// , "sb = top s0 = ", sb,'\n'\r
)\r
where\r
s0 = newStack\r
s5 = top s4\r
s6 = topn 3 s2\r
s7 = elements s2\r
+// s8 = push 10 s1\r
+// s9 = popn 10 s8\r
+// sa = topn 5 s4\r
+// sb = top s0\r