Skip to content

Instantly share code, notes, and snippets.

@ahoy-jon
Created September 24, 2012 09:35
Show Gist options
  • Save ahoy-jon/3775129 to your computer and use it in GitHub Desktop.
Save ahoy-jon/3775129 to your computer and use it in GitHub Desktop.
isBalanced
def balance(chars: List[Char]): Boolean = {
@scala.annotation.tailrec
def balance(chars: List[Char], stack:List[Unit]): Boolean = (chars, stack) match {
case (Nil , Nil) => true
case (Nil , _ ) => false
case (')':: _, Nil) => false
case (head::tail, stack) => head match {
case '(' => balance(tail, () :: stack)
case ')' => balance(tail, stack.tail )
case _ => balance(tail, stack )
}
}
balance(chars, Nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment