summaryrefslogtreecommitdiff
path: root/Library/Stack.hs
diff options
context:
space:
mode:
authorJed Barber <jjbarber@y7mail.com>2012-09-13 02:14:04 +1000
committerJed Barber <jjbarber@y7mail.com>2012-09-13 02:14:04 +1000
commitea3f1f7736c57747e743a5106b917d2853c62f57 (patch)
treef6aa645c04b3606694184fa9b949abd020550643 /Library/Stack.hs
parentec95ea382132f0702e5046a721e4d8ea4cdc82e3 (diff)
Cleaner directory structure, addition of make clean, grouping of library modules into a single package
Diffstat (limited to 'Library/Stack.hs')
-rw-r--r--Library/Stack.hs39
1 files changed, 39 insertions, 0 deletions
diff --git a/Library/Stack.hs b/Library/Stack.hs
new file mode 100644
index 0000000..7292869
--- /dev/null
+++ b/Library/Stack.hs
@@ -0,0 +1,39 @@
+module Library.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)