Question

I've got the following Scala code(ported from a Java one):

import scala.util.control.Breaks._

object Main {

def pascal(col: Int, row: Int): Int = {
    if(col > row) throw new Exception("Coloumn out of bound");
    else if (col == 0 || col == row) 1;
    else pascal(col - 1, row - 1) + pascal(col, row - 1);
}
def balance(chars: List[Char]): Boolean = {
  val string: String = chars.toString()
  if (string.length() == 0) true;
  else if(stringContains(")", string) == false && stringContains("(", string) == false) true;
  else if(stringContains(")", string) ^ stringContains("(", string)) false;
  else if(getFirstPosition("(", string) > getFirstPosition(")", string)) false;
  else if(getLastPosition("(", string) > getLastPosition(")", string)) false;
  else if(getCount("(", string) != getCount(")", string)) false;
  var positionOfFirstOpeningBracket = getFirstPosition("(", string);
  var openingBracketOccurences = 1; //we already know that at the first position there is an opening bracket so we are incrementing it right away with 1 and skipping the firstPosition variable in the loop
  var closingBracketOccurrences = 0;
  var positionOfClosingBracket = 0;
  breakable {
    for(i <- positionOfFirstOpeningBracket + 1 until string.length()) {

        if (string.charAt(i) == ("(".toCharArray())(0)) {
            openingBracketOccurences += 1;
        }
        else if(string.charAt(i) == (")".toCharArray())(0) ) {
            closingBracketOccurrences += 1;
        }
        if(openingBracketOccurences - closingBracketOccurrences == 0) { //this is an important part of the algorithm. if the string is balanced and at the current iteration opening=closing that means we know the bounds of our current brackets.
            positionOfClosingBracket = i; // this is the position of the closing bracket
            break;
        }  

    }
  }
  val insideBrackets: String = string.substring(positionOfFirstOpeningBracket + 1, positionOfClosingBracket)
  balance(insideBrackets.toList) && balance( string.substring(positionOfClosingBracket + 1, string.length()).toList)

  def getFirstPosition(character: String, pool: String): Int = 
    {
        for(i <- 0 until pool.length()) {
            if (pool.charAt(i) == (character.toCharArray())(0)) {
                i;
            }
        }
        -1;
    }

def getLastPosition(character: String, pool: String): Int =
{
    for(i <- pool.length() - 1 to 0 by -1) {
        if (pool.charAt(i) == (character.toCharArray())(0)) {
            i;
        }
    }
    -1;
}

//checks if a string contains a specific character
def stringContains(needle: String, pool: String): Boolean =  {
    for(i <- 0 until pool.length()) {
        if(pool.charAt(i) == (needle.toCharArray())(0)) true;
    }
    false;
}

//gets the count of occurrences of a character in a string
def getCount(character: String, pool: String) = {
    var count = 0;
    for ( i <- 0 until pool.length()) {
        if(pool.charAt(i) == (character.toCharArray())(0)) count += 1;
    }
    count;
}
}
}

The problem is that the Scala IDE(lates version for Scaal 2.10.1) gives the following error at line 78(on which there is a closin brace): "Type mismatch; Found Unit, expected Boolean". I really can't understand what the actual problem is. The warning doesn't give any information where the error might be.

Was it helpful?

Solution

In Scala (and most other functional languages) the result of a function is the value of the last expression in the block. The last expression of your balance function is the definition of function getCount, which is of type Unit (the Scala equivalent of void), and your function is declared as returning a Boolean, thus the error.

In practice, you've just screwed up your brackets, which would be obvious if you used proper indentation (Ctrl+A, Ctrl+Shift+F in scala-ide).

OTHER TIPS

To make it compile, you can put the following two lines at the end of the balance method instead of in the middle:

val insideBrackets: String = string.substring(positionOfFirstOpeningBracket + 1, positionOfClosingBracket)
balance(insideBrackets.toList) && balance(string.substring(positionOfClosingBracket + 1, string.length()).toList)

You would also have to put the inner functions at the top of balance I think -- such as getCount.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top