Scala immutable variables and printing

后端 未结 2 1285
予麋鹿
予麋鹿 2021-01-19 23:01

Currently taking a class that\'s using Scala which I\'ve never used before, so the syntax and itself is new.

I\'m working on a simple division function but am runnin

2条回答
  •  温柔的废话
    2021-01-19 23:57

    For completeness, putting more idiomatic recursive definition here:

    def div(m: Int, n: Int): Int = {
      @annotation.tailrec
      def loop(count: Int, sub: Int): Int = 
        if (sub < n) count
        else loop(count + 1, sub - n)
    
      loop(0, m)
    }
    

提交回复
热议问题