Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 15611

Haskell function to verify parentheses matching

$
0
0

I need to write a function par :: String -> Bool to verify if a given string with parentheses is matching using stack module.

Ex:

par "(((()[()])))" = Truepar "((]())" = False

Here's my stack module implementation:

module Stack (Stack,              push, pop, top,              empty, isEmpty)    wheredata Stack a = Stk [a]             deriving (Show)push :: a -> Stack a -> Stack apush x (Stk xs) = Stk (x:xs)pop :: Stack a -> Stack apop (Stk (_:xs)) = Stk xspop _ = error "Stack.pop: empty stack"top :: Stack a -> atop (Stk (x:_)) = xtop _ = error "Stack.top: empty stack"empty :: Stack aempty = Stk []isEmpty :: Stack a -> BoolisEmpty (Stk [])= TrueisEmpty (Stk _) = False

So I need to implement a par function that would test a string of parentheses and say if the parentheses in it are balanced or not. How can I do that using a stack?


Viewing all articles
Browse latest Browse all 15611

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>