括号在scala中平衡函数

时间:2013-06-19 09:42:54

标签: scala

我写了一个平衡括号的函数,但我遇到了一些问题

def subfunc(left: Int, chars: List[Char]): Boolean = {
  if (chars.isEmpty) {
    if (left == 0) { println("true"); true }
    else false
  **} else {**
    if (chars.head == '(') subfunc(left + 1, chars.tail)
    else if (chars.head == ')') {
      if (left > 0) subfunc(left - 1, chars.tail)
      else false
    } else
      subfunc(left, chars.tail)
  }
}

当func是这样时,perfermance就可以了,但如果我是} else { 并且代码变成了这个

  def subfunc(left: Int, chars: List[Char]): Boolean = {
    if (chars.isEmpty) {
      if (left == 0) { println("true"); true }
      else false
    }
    println("come to here")
    if (chars.head == '(') subfunc(left + 1, chars.tail)
    else if (chars.head == ')') {
      if (left > 0) subfunc(left - 1, chars.tail)
      else false
    } else
      subfunc(left, chars.tail)
  }  

和测试崩溃

  subfunc(0, chars)                               //> come to here?
                                                  //| come to here?
                                                  //| come to here?
                                                  //| come to here?
                                                  //| come to here?
                                                  //| true
                                                  //| come to here?
                                                  //| java.util.NoSuchElementException: head of empty list
                                                  //|   at scala.collection.immutable.Nil$.head(List.scala:337)
                                                  //|   at scala.collection.immutable.Nil$.head(List.scala:334)
                                                  //|   at recfun.expriment$$anonfun$main$1.subfunc$1(recfun.expriment.scala:22)
                                                  //| 
                                                  //|   at recfun.expriment$$anonfun$main$1.apply$mcV$sp(recfun.expriment.scala:
                                                  //| 35)
                                                  //|   at org.scalaide.worksheet.runtime.library.WorksheetSupport$$anonfun$$exe
                                                  //| cute$1.apply$mcV$sp(WorksheetSupport.scala:76)
                                                  //|   at org.scalaide.worksheet.runtime.library.WorksheetSupport$.redirected(W
                                                  //| orksheetSupport.scala:65)
                                                  //|   at org.scalaide.worksheet.runtime.library.WorksheetSupport$.$execute(Wor
                                                  //| ksheetSupport.scala:75)
                                                  //|   at recfun.expriment$.main(recfun.expriment.scala:3)
                                                  //|   at recfun.expriment.main(recfun.expriment.scala)

似乎该程序来到subfunc(0,emptylist) 但为什么它会在行之后“来到这里”

if (left == 0) { println("true"); true}

被执行了吗?

1 个答案:

答案 0 :(得分:1)

因为scala仅返回最后一个函数表达式 您的第一个代码类似于 if(chars.isEmpty) 的任一分支的返回结果。你的第二个代码类似于先执行if,然后返回chars.head == '('. 的任一分支的结果。就是这样 - 当你以这种方式编写代码时,函数在第一个if之后不会急切地返回