Skip to content

Instantly share code, notes, and snippets.

@gsriram7
Created May 18, 2015 07:07
Show Gist options
  • Save gsriram7/4232de581448438b2830 to your computer and use it in GitHub Desktop.
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
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