0

New to Scala and trying to figure out recursion.

Having the fallowing definitions in my session:

def inc(n: Int) = n + 1
def dec(n: Int) = n – 1

How could I redefine function below to use recursion inc and dec?

add(n: Int, m: Int) = n + m

I'm interested in learning both regular recursion and tail recursion.

Thanks

4
  • 2
    Neither of those functions use recursion. What is your definition of recursion? Recursion in Scala is no different than other languages, really Commented Oct 13, 2016 at 5:18
  • @cricket_007 I'm aware. Trying to find a solution that only use recursion, inc, dec Commented Oct 13, 2016 at 5:23
  • What is your expected output for what input? Commented Oct 13, 2016 at 5:27
  • 1
    add(2,2) should return 4 Commented Oct 13, 2016 at 5:30

3 Answers 3

4

How about this:

scala> def inc(n: Int) = n + 1
inc: (n: Int)Int

scala> def dec(n: Int) = n - 1
dec: (n: Int)Int

scala> def add(n: Int, m: Int): Int = m match {
     |   case 0           => n
     |   case _ if m > 0  => add(inc(n), dec(m))
     |   case _           => add(dec(n), inc(m))
     | }
add: (n: Int, m: Int)Int

scala> add(100, 99)
res0: Int = 199

scala> add(100, -99)
res1: Int = 1

Or there is another solution, which is an implementation of the Peano axioms.

scala> def add2(n: Int, m: Int): Int = m match {
     |   case 0           => n
     |   case _ if m > 0  => inc(add2(n, dec(m)))
     |   case _           => dec(add2(n, inc(m)))
     | }
add2: (n: Int, m: Int)Int
Sign up to request clarification or add additional context in comments.

Comments

0

Tail Recursion has 3 parts as far as I'm concerning:

  • Condition to end recursion
  • return value if the condition is met, the returned value is one (or derived from) the parameters of the tail recursive function
  • and the call to itself if the condition is unmet.

sample:

def inc(n: Int) = n + 1
def dec(n: Int) = n - 1


def add(n:Int, m:Int, sum: Int):Int = {
  //condition to break/end the recursion
  if (m <= 0) {
    // returned value once condition is met. This is the final output of the recursion
    sum
  } else {
    //call to itself once condition is unmet
    add(inc(n), dec(m), n + m)
  }
}

as you can see, it feels like you are doing while loop but more functional way.

on recursion, calls are stack which result to having it's call stack size as depth of the recursive calls (which can result to stackoverflowexception) on tail recursion it is like how while loop is interpreted.

sample of recursion:

def addAllNumberFromNToZero(n:Int):Int = {
  if (m <= 0) {
    sum
  } else {
    n + add(n - 1)
  }
}

Comments

0

Using regular recursion, you could try something like:

  def inc(n: Int) = n + 1
  def dec(n: Int) = n - 1

  def add(n: Int, m: Int): Int = {
    if (m == 0) n
    else add(inc(n), dec(m))
  }

The add function recursively calls itself add, each time incrementing n and reducing m. The recursion stops when m reaches zero, at which point m is returned.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.