2017-08-05 25 views
0

我是一个Scala初学者。我在学习Functional Programming in Scala。但我面对这个问题时,我做的第一exercise.here是第一个练习:我的Scala代码在第一个练习的Scala中的函数式编程原理有什么问题

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

在三角形的边缘都是1号,以及三角形内的数值 是的总和它上面的两个数字。编写一个 函数,通过递归过程计算帕斯卡三角形的元素。

通过执行main.scala中的pascal函数 来完成此练习,该函数采用列c和行r,从0开始计数并返回该三角形中该点处的 数字。例如,pascal(0,2)= 1, pascal(1,2)= 2和pascal(1,3)= 3。

这是我的第一个代码:

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 = ??? 
    } 

,但我得到这个结果

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 

然后我试图改变我的代码,我也得到正确的结果。

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 = ??? 
    } 

我想知道为什么我的第一个代码是worng?

回答

0
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. 

希望它回答你的问题。

+0

谢谢,我误解了if语句的then部分已经执行后,函数结束并返回结果。像c编程语言一样。 –

+0

没问题,如果你对每个块的条件都使用了大括号将会很好,如果它对你有帮助,你可以投票。 –

+0

对不起,我不能投票,因为我的名声较差。 –

1

在第二个代码中,if语句的值是该函数的结果。在第一个函数中,i是该函数的结果,并且不使用if语句的值。所以重要的是if语句对i所做的事情。 if语句的then部分没有做任何事情来i,所以如果当时的部分取,其结果将是i于前值,这将是0

+0

为什么if语句的then部分对我没有任何帮助? –

+0

@天才猪为什么会呢?它根本没有提到“我”。 – sepp2k

+0

'@ sepp2k'例如,我认为pascal(1,1)将返回1,因为if语句的then部分已经被执行。 –