【问题标题】:Scala syntax in nested functions嵌套函数中的 Scala 语法
【发布时间】:2016-06-27 01:31:32
【问题描述】:

这道题主要是语法问题

我有一个程序来计算括号的数量以查看它们是否匹配,因此必须有相等数量的“(”和“)”,在“(”之前没有“)”

def balance(chars: List[Char]): Boolean = {
  if (chars.isEmpty) throw new NoSuchElementException

  def helper(chars: List[Char], num: Int = 0, check: Int = 1): (Int, Int) = {
    if (num < 0) check = 0
    if (chars.head == "(") return helper(chars.tail, num + 1)
    if (chars.head == ")") return helper(chars.tail, num - 1)
    else return(chars.tail, num)
  }

  if (num == 0 && check == 1) return true
  else return false
}

这就是我想要做的:

  1. 定义一个函数,根据一个列表返回一个Boolean true 或 false

  2. 如果列表为空,则返回无内容

  3. 定义一个辅助函数,返回两个整数 check 和 num,可用作判断括号是否匹配的条件

  4. 如果num小于零,则表示左括号前有右括号,不可接受,检查已改为0

  5. 如果头部的字符值是左括号,我调用helper函数,将num的值加一

  6. 如果头部的字符值是右括号,我调用辅助函数,将num的值减一

  7. 如果头部的字符值既不是右括号也不是左括号,我调用辅助函数并将num值减一

    这个函数的重点是返回一个num并检查变量以返回它嵌套的函数的布尔值

  8. 在外部函数中,我想确定左括号之前是否有右括号(check == 0),或者它们不匹配(num != 1

我应该进行哪些语法更改?我喜欢这种逻辑思考事物的方式,所以请不要试图改变我的逻辑,除非我不接受函数式风格,并且仍然更加程序化地思考。

【问题讨论】:

  • 为什么要进行语法更改?
  • 要修复的地方太多了,你说不要更改复杂的logic,这是不必要的。您不仅否认函数式编程,而且否认 scala 本身。尽管您不想改变,但仍有大量有用的员工。回到你以前使用的语言。另外,你没有提到问题是什么。您的代码实际上同时具有问题语法一和逻辑一。人们如何回答这个问题?
  • check的目的是什么?您使用它的方式不起作用,并且不清楚您想要什么。您不能将值重新分配给参数值。值numcheck 未在封闭方法中定义。此外,您不会在封闭方法中的任何位置调用 helper 方法。
  • 我添加变量检查的原因是如果右括号在左括号之前设置一个值,即如果num小于0,那么检查将设置为0而不是1 ,这意味着列表不平衡。我之所以打出上面的步骤是因为这是我在思考这个过程的方式,如果我没有使用函数式编程和 Scala 的好处,那么请告诉我,也告诉我我应该在一种不同的方式。如果这个过程对我来说是可以理解的,那将会很有帮助。

标签: scala syntax


【解决方案1】:

我做了一些小改动以使您的代码正常工作:

def balance(chars: List[Char]): Boolean = {
  if (chars.isEmpty) throw new NoSuchElementException

  @scala.annotation.tailrec
  def helper(chars: List[Char], num: Int = 0): Boolean = chars match {
    case _ if num < 0 => false
    case head :: tail if head == '(' =>
      helper(tail, num + 1)
    case head :: tail if head == ')' =>
      helper(tail, num - 1)
    case head :: tail =>
      helper(tail, num)
    case Nil => num == 0
  }
  helper(chars)
}

balance("()()()()(".toList) //res0: Boolean = false
balance("(adsads)()()()sdfsdfsdf".toList) //res1: Boolean = true
balance("(()()()(asdasd))".toList) //res2: Boolean = true
balance("asdasd".toList) //res3: Boolean = true
balance("()()(()(".toList) //res4: Boolean = false
balance("".toList) // java.util.NoSuchElementException

或者没有模式匹配:

def balance(chars: List[Char]): Boolean = {
  if (chars.isEmpty) throw new NoSuchElementException

  @scala.annotation.tailrec
  def helper(chars: List[Char], num: Int = 0): Boolean = {
    if (num < 0)
      false
    else if(chars.isEmpty)
      num == 0
    else if(chars.head == '(')
      helper(chars.tail, num + 1)
    else if(chars.head == ')')
      helper(chars.tail, num - 1)
    else
      helper(chars.tail, num)
  }
  helper(chars)
}

balance("()()()()(".toList) //res0: Boolean = false
balance("(adsads)()()()sdfsdfsdf".toList) //res1: Boolean = true
balance("(()()()(asdasd))".toList) //res2: Boolean = true
balance("asdasd".toList) //res3: Boolean = true
balance("()()(()(".toList) //res4: Boolean = false
balance("".toList) // java.util.NoSuchElementException

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多