Question

Haskell Functional Programming Language: Write a function nestedParens that takes a string argument and returns true...

Haskell Functional Programming Language:

Write a function nestedParens that takes a string argument and returns true if it is a nesting of zero or more pairs of parentheses, e.g. "((()))" should return True ; "()()" or "(()))" should return False . Use recursion for this problem.

nestedParens :: String -> Bool

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code in c++ for above problem-

#include <iostream>
using namespace std;

int nestedParens(string S)
{
int current_max = 0; // current count
int max = 0; // overall maximum count
int n = S.length();
  
for (int i = 0; i< n; i++)
{
if (S[i] == '(')
{
current_max++;
  
if (current_max> max)
max = current_max;
}
else if (S[i] == ')')
{
if (current_max>0)
current_max--;
else
return -1;
}
}
  
// finally check for unbalanced string
if (current_max != 0)
return -1;
  
return max;
}
  
int main()
{
string s = "()()";
if(nestedParens(s)==1){
   cout<<"false";
   }else{
       cout<<"true";
   }
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Haskell Functional Programming Language: Write a function nestedParens that takes a string argument and returns true...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT