-1

我有以下 Scala 代码(从 Java 移植而来):

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;
}
}
}

问题是 Scala IDE(Scaal 2.10.1 的最新版本)在第 78 行(上面有一个闭合括号)给出了以下错误:“类型不匹配;找到单元,预期布尔值”。我真的无法理解实际问题是什么。警告没有给出错误可能在哪里的任何信息。

4

2 回答 2

5

在 Scala(和大多数其他函数式语言)中,函数的结果是块中最后一个表达式的值。函数的最后一个表达式是balance function 的定义getCount,它是类型Unit(Scala 等效的void),并且您的函数被声明为返回 a Boolean,因此是错误。

在实践中,您刚刚搞砸了括号,如果您使用正确的缩进(在 scala-ide 中为 Ctrl+A、Ctrl+Shift+F),这将是显而易见的。

于 2013-03-26T14:27:17.063 回答
1

要使其编译,您可以将以下两行放在balance方法的末尾而不是中间:

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

您还必须将内部函数放在balance我认为的顶部 - 例如getCount.

于 2013-03-26T14:31:13.590 回答