Advertisement
Guest User

Untitled

a guest
May 29th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.48 KB | None | 0 0
  1. /**
  2.     * Exercise 2
  3.     */
  4.   def balance(chars: List[Char]): Boolean = {
  5.  
  6.     def balanceInner(chars: List[Char], acc: Int = 0): Boolean = {
  7.       def accUpgrade(character: Char) = character match {
  8.         case '(' => acc + 1
  9.         case ')' => acc + -1
  10.         case _ => acc
  11.       }
  12.  
  13.       if (acc < 0) false
  14.       else if (chars.isEmpty)
  15.         if (acc == 0) true else false
  16.       else balanceInner(chars.tail, accUpgrade(chars.head))
  17.     }
  18.  
  19.     balanceInner(chars)
  20.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement