Boris
Boris

Reputation: 75

What's wrong with my scala code in Functional Programming Principles in Scala of the first exercise

I'm a scala beginner. I'm learning Functional Programming in Scala. But I face this problem when I do the first exercise.here is the first exercise:

    1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1

The numbers at the edge of the triangle are all 1, and each number inside the triangle is the sum of the two numbers above it. Write a function that computes the elements of Pascal’s triangle by means of a recursive process.

Do this exercise by implementing the pascal function in Main.scala, which takes a column c and a row r, counting from 0 and returns the number at that spot in the triangle. For example, pascal(0,2)=1, pascal(1,2)=2 and pascal(1,3)=3.

here is my first code:

package recfun

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 i = 0

  /**
    * Exercise 1
    */
  def pascal(c: Int, r: Int): Int = {
    if(c==r||c==0)
      1
    else
      i = pascal(c - 1, r - 1) + pascal(c, r - 1)
    i
  }

  /**
   * Exercise 2
   */
    def balance(chars: List[Char]): Boolean = ???

  /**
   * Exercise 3
   */
    def countChange(money: Int, coins: List[Int]): Int = ???
  }

but I get this result

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 

then I try to change my code,and I get correct result.

package recfun

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()
    }
  }


  /**
    * Exercise 1
    */
  def pascal(c: Int, r: Int): Int = {
    if(c==r||c==0)
      1
    else
       pascal(c - 1, r - 1) + pascal(c, r - 1)
  }

  /**
   * Exercise 2
   */
    def balance(chars: List[Char]): Boolean = ???

  /**
   * Exercise 3
   */
    def countChange(money: Int, coins: List[Int]): Int = ???
  }

I want to know why my first code is worng?

Upvotes: 0

Views: 459

Answers (2)

Sagar balai
Sagar balai

Reputation: 479

There is problem in your first pascal function
i=0
def pascal(c: Int, r: Int): Int = {
    if(c==r||c==0)
      1
    else
      i = pascal(c - 1, r - 1) + pascal(c, r - 1)
    i
  }

In if-else block always var 'i' is returned which has value as 0. In else condition it is assigned but as recurring pascal will return initial 'i' value (0), it will be always 0.

It is not bad practise to use global variable in function only for returning any value so second approach is more correct still if you want to return value with global variable at least assign value in if and else block as below

i=0
def pascal(c: Int, r: Int): Int = {
    if(c==r||c==0)
      i=1    // this will be assigned for the first time and when r==c
    else
      i = pascal(c - 1, r - 1) + pascal(c, r - 1) // this will be assigned for subsequent calculations
    i  // it will return value which is calculated in if else block
  }

**It is recommended to use proper curly braces {} to avoid such confusion.** 

def pascal(c: Int, r: Int): Int = {
    if(c==r||c==0){      
        i=1
    } else{    
       i = pascal(c - 1, r - 1) + pascal(c, r - 1)
   } 
    i
  }

In the second code, you just have if-else block and no statement after this block and scala is smart and return from if and else block only.

Hope it answer your question.

Upvotes: 1

sepp2k
sepp2k

Reputation: 370445

In the second code the value of the if-statement is the result of the function. In the first function, i is the result of the function and the value of the if-statement is not used. So all that matters is what the if-statement does to i. The then-part of the if-statement doesn't do anything to i, so if the then-part is taken, the result will be the previous value of i, which will be 0.

Upvotes: 1

Related Questions