0

Trying to create a pascals triangle using recursive function. Returning value is always zero.

I'm new to Scala programming and not sure if the declaring the value variable in the code is the right way. Appreciate any help with the right approach. Thanks

object Main {
  def main(args: Array[String]) {
    println("Pascal's Triangle")
    for (row <- 0 to 10) {
      for (col <- 0 to row)
        print(pascal(col, row) + " ")
      println()

    }
  }

  var value: Int = 0
  def pascal(c: Int, r: Int): Int = {
    if (c ==0) 1
    else if (c == r ) 1
    else
      for (col <- c-1 to c) {
        value +=   pascal(col, r - 1)
      }
      value
    }
  }

Actual Result

Pascal's Triangle
0 
0 0 
0 0 0 
0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 0 
0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 

Expected Result

Pascal's Triangle
1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
1 5 10 10 5 1 
1 6 15 20 15 6 1 
1 7 21 35 35 21 7 1 
1 8 28 56 70 56 28 8 1 
1 9 36 84 126 126 84 36 9 1 
1 10 45 120 210 252 210 120 45 10 1

2 Answers 2

2

Scala style tries to avoid mutable data (i.e. var).

def pascalTriangle(height :Int) :Unit =
  Iterator.iterate(Vector(1))(v => (0+:v:+0).sliding(2).map(_.sum).toVector)
          .take(height)          //take only as many as needed
          .map(_.mkString(" "))  //turn Vector into space separated String
          .foreach(println)      //force the calculations

Here we create an infinite collection of Vectors, each one longer than the previous. Each Vector is processed to create the required sums, but none of that happens until the foreach() because an Iterator is lazy.

Sign up to request clarification or add additional context in comments.

Comments

1

You should avoid using var. In this case, you don't need it. Avoid var, use val. Such is good functional programming practice.

object Pascal {

  def main(args: Array[String]) {
    println("Pascal's Triangle")
    for (row <- 0 to 10) {
      for (col <- 0 to row)
        print(pascal(col, row) + " ")
      println()

    }
  }

  def pascal(c: Int, r: Int): Int = {
    if (c ==0) 1
    else if (c == r ) 1
    else {
      val intermediate = for (col <- c - 1 to c) yield pascal(col, r - 1)
      intermediate.sum
    }
  }
}

3 Comments

Thanks this was exactly what I was looking for. I have question, in procedural programming incrementing a variable is a frequent operation(for example counting), how to handle such kind of scenarios using a val variable?
Usually, in imperative programming, you increment variable as part of a loop. In functional programming, instead of incrementing a variable in a loop, you increment a val and pass it as a parameter to the next call of a recursive function. Another case is if you just want the keep track of the index of an item in a sequence. If you just want the index of a value in a sequence, use the zipWith function.
@Midhun If you don't understand the first part of my previous answer, try doing some example problems relating to recursion. It will demonstrate what I mean by incrementing a value and passing it as a parameter to a recursive function.

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.