summaryrefslogtreecommitdiff
path: root/Library/Stack.hs
blob: 99cd8e1412b4e3142e3d22845f8f7ddba434b6e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module Library.Stack (
	Stack,
    empty,
    at,
    pop,
    (<:>),
    size,
    diff
    ) where


import Data.List


data Stack a = Stack [a] deriving (Eq)


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)


size :: Stack a -> Int
size (Stack list) = length list


diff :: (Eq a) => Stack a -> Stack a -> Stack a
diff (Stack one) (Stack two) = Stack (one \\ two)