scala中的堆栈溢出递归更改硬币函数

时间:2014-05-08 07:01:22

标签: scala recursion stack-overflow

查看来自coursera的scala课程提供的资源,我发现了一个应该工作的算法(逻辑应该可行),但是当在scala中实现时,它会返回堆栈溢出。我想我知道堆栈是什么,因为我在C中玩了一段时间,但我不明白以下函数如何耗尽这么多内存:

      def countChange(money: Int, coins: List[Int]): Int = {


    def cc(amount: Int, list: List[Int]): Int = {
      if (amount == 0) 1
      if (amount < 0 || list.isEmpty) 0

      return cc(amount - list.head, list) + cc(amount, list.tail)

    }

    cc(money, coins)






  }

这是错误:

Exception in thread "main" java.lang.StackOverflowError
at java.lang.Integer.valueOf(Unknown Source)
at scala.runtime.BoxesRunTime.boxToInteger(BoxesRunTime.java:70)
at recfun.Main$.cc$1(Main.scala:78)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)
at recfun.Main$.cc$1(Main.scala:80)

这是对函数的调用:

 println ("" + countChange(10,List(2,3,5)))

1 个答案:

答案 0 :(得分:3)

错误的return行为(混合表达式和陈述)

没有return关键字:

def countChange(money: Int, coins: List[Int]): Int = {
  def cc(amount: Int, list: List[Int]): Int = {
    if (amount == 0) 1
    else if (amount < 0 || list.isEmpty) 0
    else cc(amount - list.head, list) + cc(amount, list.tail)
  }
  cc(money, coins)
}   

或使用return关键字:

def countChange1(money: Int, coins: List[Int]): Int = {
  def cc(amount: Int, list: List[Int]): Int = {
    if (amount == 0) return 1
    if (amount < 0 || list.isEmpty) return 0
    return cc(amount - list.head, list) + cc(amount, list.tail)
  }
  cc(money, coins)
}