-
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathStack.hs
More file actions
25 lines (19 loc) · 606 Bytes
/
Stack.hs
File metadata and controls
25 lines (19 loc) · 606 Bytes
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
module DataStructures.Stack where
data Stack t = Empty | Node t (Stack t) deriving(Show)
push :: Stack t -> t -> Stack t
push Empty n = (Node n Empty)
push (Node t tail) n = (Node n (Node t tail))
pop :: Stack t -> Stack t
pop Empty = Empty
pop (Node t tail) = tail
stackToList :: Stack t -> [t]
stackToList Empty = []
stackToList (Node t tail) = t:stackToList tail
listToStack :: [t] -> Stack t
listToStack [] = Empty
listToStack (x:xs) = aux (Node x Empty) xs
where
aux s [] = s
aux s (x:xs) = aux (push s x) xs
main = do
print (listToStack [1..5])