Skip to content

Instantly share code, notes, and snippets.

@lazyval
Created October 3, 2012 17:53
Show Gist options
  • Save lazyval/3828597 to your computer and use it in GitHub Desktop.
Save lazyval/3828597 to your computer and use it in GitHub Desktop.
bracket balanicing
object Main extends App {
println(isBalanced("(.)(.)".toList))
def isBalanced(list: List[Char]) = {
@annotation.tailrec
def countBracets(string: List[Char], count: Int = 0): Int = {
string match {
case '('::tail => countBracets(tail, count+1)
case ')'::tail => countBracets(tail, count-1)
case x::tail => countBracets(tail, count) // just skip if not bracet
case Nil => count
}
}
countBracets(list) == 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment