I need to write a function par :: String -> Boolto check if a given string with parentheses matches using the stack module.
Example:
par "(((()[()])))" = True
par "((]())" = False
Here's the implementation of my module:
module Stack (Stack,
push, pop, top,
empty, isEmpty)
where
data Stack a = Stk [a]
deriving (Show)
push :: a -> Stack a -> Stack a
push x (Stk xs) = Stk (x:xs)
pop :: Stack a -> Stack a
pop (Stk (_:xs)) = Stk xs
pop _ = error "Stack.pop: empty stack"
top :: Stack a -> a
top (Stk (x:_)) = x
top _ = error "Stack.top: empty stack"
empty :: Stack a
empty = Stk []
isEmpty :: Stack a -> Bool
isEmpty (Stk [])= True
isEmpty (Stk _) = False
So, I need to implement a function parthat will check the string of parentheses and say whether the brackets are balanced in it or not. How to do this using the stack?
source
share