-3

Here is the function for balancing parenthesis in scala. I am getting

Error:(36, 10) type mismatch;
 found   : Unit
 required: Int
        a=a+1
var a = 0

      def balance(chars: List[Char]): Boolean = {
        if(chars.isEmpty)
          return Nil
        {
          if (chars.head == ')')
            a=a-1
          else (chars.head == '(')
            a=a+1
        }
        if (a == -1)
          return false
        if ((a == 1 || a == 0) && chars.tail.isEmpty!= 0)
          balance(chars.tail)
        if (a == 0 && chars.tail.isEmpty)
          return true
      }

Can anyone tell me why this error is coming?

user459
  • 111
  • 8

1 Answers1

4

The code is full of errors.

You return Nil whereas your function has return type Boolean.

You define else with condition without if.

You use ambiguous condition chars.tail.isEmpty!= 0.

Fix these and repost your code. There are more errors but at the moment it is hard to tell whether you want to call recursively and return on empty List or return on some condition. If you still have problems then, I will update my answer appropriately.

sebszyller
  • 853
  • 4
  • 10