diff options
| author | Camil Staps | 2015-02-22 22:54:02 +0100 | 
|---|---|---|
| committer | Camil Staps | 2015-02-22 22:54:02 +0100 | 
| commit | 56a5e0f968fc99cd1dc4731b0d1cc572f07d6f89 (patch) | |
| tree | e45a970e84b2bc02caf8d93f368c818b7cf3fdb6 | |
| parent | working on w3 (diff) | |
6.5 working
| -rw-r--r-- | week3/camil/.gitignore | 1 | ||||
| -rw-r--r-- | week3/camil/StdStack.icl | 38 | 
2 files changed, 37 insertions, 2 deletions
| 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
 | 
