Skip to content

Instantly share code, notes, and snippets.

@tnoda
Created April 3, 2013 10:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tnoda/5300176 to your computer and use it in GitHub Desktop.
Save tnoda/5300176 to your computer and use it in GitHub Desktop.
[再帰ドリル](https://github.com/kazu-yamamoto/recursion-drill) を Scala で書いてみる.
package recfun
object DrillOne {
// sum of arithmetic progression
def soap(x: Int): Int =
if (x == 0) 0
else soap(x - 1) + x
// factorial
def fact(x: Int): Int =
if (x == 1) 1
else fact(x - 1) * x
// multiply
def mul(m: Int, n: Int): Int =
if (n == 1) m
else mul(m, n - 1) + m
// plus
def plus(m: Int, n: Int): Int =
if (n == 0) m
else plus(m, n - 1) + 1
// minus
def minus(m: Int, n : Int): Int =
if (n == 0) m
else minus(m, n - 1) - 1
// power
def power(m: Int, n: Int): Int =
if (n == 1) m
else power(m, n - 1) * m
}
package recfun
import scala.annotation.tailrec
object DrillThree {
// less than
@tailrec
def lt(m: Int, n: Int): Boolean = (m, n) match {
case (_, 0) => false
case (0, _) => true
case (m, n) => lt(m - 1, n - 1)
}
// less than or eqaul to
@tailrec
def lteq(m: Int, n: Int): Boolean = (m, n) match {
case (0, _) => true
case (_, 0) => false
case (m, n) => lteq(m - 1, n - 1)
}
// even number
@tailrec
def isEven(n: Int): Boolean = n match {
case 0 => true
case 1 => false
case n => isEven(n - 2)
}
// divide
def divide(m: Int, n: Int): Int =
if (m < n) 0
else divide(m - n, n) + 1
// greater than
@tailrec
def gt(m: Int, n: Int): Boolean = (m, n) match {
case (0, _) => false
case (_, 0) => true
case (m, n) => gt(m - 1, n - 1)
}
// greater than or equal to
@tailrec
def gteq(m: Int, n: Int): Boolean = (m, n) match {
case (_, 0) => true
case (0, _) => false
case (m, n) => gteq(m - 1, n - 1)
}
// odd number
@tailrec
def isOdd(n: Int): Boolean = n match {
case 0 => false
case 1 => true
case n => isOdd(n - 2)
}
// remainder
@tailrec
def remainder(m: Int, n: Int): Int =
if (m < n) m
else remainder(m - n, n)
// recursive version of divide
def divide2(m: Int, n: Int): Int = {
@tailrec
def loop(acc: Int, m: Int): Int =
if (m < n) acc
else loop(acc + 1, m - n)
loop(0, m)
}
}
package recfun
import scala.annotation.tailrec
object DrillTwo {
// sum of arithmetic progression
def soap(n: Int): Int = {
@tailrec
def loop(acc: Int, n: Int): Int =
if (n == 0) acc
else loop(acc + n, n - 1)
loop(0, n)
}
// factorial
def fact(n: Int): Int = {
@tailrec
def loop(acc: Int, n: Int): Int =
if (n == 1) acc
else loop(acc * n, n - 1)
loop(1, n)
}
// multiply
def mul(m: Int, n: Int): Int = {
@tailrec
def loop(acc: Int, n: Int): Int =
if (n == 0) acc
else loop(acc + m, n - 1)
loop(0, n)
}
// plus
def plus(m: Int, n: Int): Int = {
@tailrec
def loop(acc: Int, n: Int): Int =
if (n == 0) acc
else loop(acc + 1, n - 1)
loop(m, n)
}
// minus
def minus(m: Int, n: Int): Int = {
@tailrec
def loop(acc: Int, n: Int): Int =
if (n == 0) acc
else loop(acc - 1, n - 1)
loop(m, n)
}
// power
def power(m: Int, n: Int): Int = {
@tailrec
def loop(acc: Int, n: Int): Int =
if (n == 0) acc
else loop(acc * m, n - 1)
loop(1, n)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment