-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.hs
More file actions
95 lines (79 loc) · 2.09 KB
/
Stack.hs
File metadata and controls
95 lines (79 loc) · 2.09 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
{- |
Module : Stack.hs
Description : Module implements the stack data structure
Copyright : (c) David Oniani
License : MIT License
Maintainer : onianidavid@gmail.com
Stability : provisional
Portability : portable
For more information, follow the link below.
https://en.wikipedia.org/wiki/Stack_(abstract_data_type)
-}
module Stack
( Stack (..)
, empty
, push
, top
, pop
, clear
, Stack.null
) where
import qualified Data.Sequence as S
newtype Stack = Stack (S.Seq Integer) deriving (Eq)
-- Show the stack
instance Show Stack
where
show (Stack x) = "Stack " ++ drop 9 (show x)
-- | Create the empty stack
empty :: Stack
empty = Stack S.empty
-- | Get the size of the stack
size :: Stack -> Integer
size (Stack x) = toInteger (S.length x)
-- | Push the item onto the stack
push :: Stack -> Integer -> Stack
push (Stack x) y = Stack (x S.|> y)
-- | Get the top of the stack
top :: Stack -> Integer
top (Stack x)
| S.null x = error "Cannot get the top of the empty stack!"
| otherwise = S.index x (length x - 1)
-- | Pop the item from the the stack
pop :: Stack -> Stack
pop (Stack x)
| S.null x = error "Cannot pop the empty stack!"
| otherwise = Stack (S.deleteAt (length x - 1) x)
-- | Clear the stack
clear :: Stack -> Stack
clear (Stack x) = Stack S.empty
-- | Check if the stack is empty
null :: Stack -> Bool
null (Stack x) = S.null x
main :: IO ()
main = do
let stack1 = Stack (S.fromList [0..9])
let stack2 = pop stack1
let stack3 = push stack1 2
let stack4 = clear stack1
print empty
print stack1
print stack2
print stack3
print stack4
putStr "\n"
print (size stack1)
print (size stack2)
print (size stack3)
print (size stack4)
putStr "\n"
print (top stack1)
print (top stack2)
print (top stack3)
-- print (top stack4) -- Error: Cannot get the top of the empty stack!
putStr "\n"
print (Stack.null stack1)
print (Stack.null stack2)
print (Stack.null stack3)
print (Stack.null stack4)
print(stack1 == stack1)
print(stack1 == stack2)