summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJed Barber <jjbarber@y7mail.com>2012-05-29 07:31:11 +1000
committerJed Barber <jjbarber@y7mail.com>2012-05-29 07:31:11 +1000
commit3bb617aba627d391b422ee10c799ac51524a2bdb (patch)
tree83a56c163fed924c0b93840f271bddef6574048e
parent5faabaf240316300e48cf37eae0d5e2bfd0ec66c (diff)
Moved Stack datatype to separate file
-rw-r--r--Stack.hs39
1 files changed, 39 insertions, 0 deletions
diff --git a/Stack.hs b/Stack.hs
new file mode 100644
index 0000000..2b369b7
--- /dev/null
+++ b/Stack.hs
@@ -0,0 +1,39 @@
+module Stack (
+ Stack,
+ empty,
+ at,
+ pop,
+ (<:>)
+ ) where
+
+
+import Data.List
+
+
+data Stack a = Stack [a]
+
+
+instance Show a => Show (Stack a) where
+ show (Stack x) = "Stack:\n" ++ intercalate "\n" (map (show) x) ++ "\n\n"
+
+
+infixr 9 <:>
+
+
+empty :: Stack a
+empty = Stack []
+
+
+at :: Stack a -> Int -> Maybe a
+at (Stack list) index =
+ if (index < length list && index >= 0)
+ then Just (list!!index)
+ else Nothing
+
+
+pop :: Int -> Stack a -> Stack a
+pop n (Stack list) = Stack (drop n list)
+
+
+(<:>) :: a -> Stack a -> Stack a
+x <:> (Stack list) = Stack (x : list)