Created
May 18, 2015 07:07
-
-
Save gsriram7/4232de581448438b2830 to your computer and use it in GitHub Desktop.
Here is my solution for exercise #2 using recursion and pattern matching.. Easy to understand
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def parenthesis(input: List[Char], count: Int):Boolean ={ | |
input match { | |
case Nil if count==0 => true | |
case Nil if count!=0 => false | |
case head :: tail if count==(-1) => false | |
case head :: tail if head == '('=> parenthesis(tail, count + 1) | |
case head :: tail if head == ')'=> parenthesis(tail, count - 1) | |
case head :: tail if head!='(' && head!=')' => parenthesis(tail, count) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment