Martin Odersky Scala编程公开课 第一周作业

来源:互联网 发布:js隐藏与显示 编辑:程序博客网 时间:2024/05/20 18:48

Functional Programming Principles in Scala 
by Martin Odersky

Martin教授是scala语言的creator,在coursera上面有scala课程。本文是第一周的作业。

作业应该做Eclipse里面编辑,使用WorkSheet实时检查。然后使用sbt运行styleCheck,test命令进行测试,run命令来运行main函数,如果没有错误,就可使用submit命令提交作业了。

本周作业不难。
pascal三角需要注意运算过程中尽量不要超出Int的表示范围;我在程序中使用了一个小技巧来做到这一点。
balance程序需要使用多个if else 来控制流程。
countChange则使用迭代的方法实现,如果没有想起来这个方法会比较难。还要注意程序什么条件下终止。

作业要求:
Download the recfun.zip handout archive file and extract it somewhere on your machine.This assignment counts towards your final grade. Please refer to the Grading Policy for more details.Do not forget to submit your work using the submit task from SBT. Please refer to the example assignment for instructions.Exercise 1: Pascal’s TriangleThe following pattern of numbers is called Pascal’s triangle.    1   1 1  1 2 1 1 3 3 11 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.def pascal(c: Int, r: Int): IntExercise 2: Parentheses BalancingWrite a recursive function which verifies the balancing of parentheses in a string, which we represent as a List[Char] not a String. For example, the function should return true for the following strings:(if (zero? x) max (/ 1 x))I told him (that it’s not (yet) done). (But he wasn’t listening)The function should return false for the following strings::-)())(The last example shows that it’s not enough to verify that a string contains the same number of opening and closing parentheses.Do this exercise by implementing the balance function in Main.scala. Its signature is as follows:def balance(chars: List[Char]): BooleanThere are three methods on List[Char] that are useful for this exercise:chars.isEmpty: Boolean returns whether a list is emptychars.head: Char returns the first element of the listchars.tail: List[Char] returns the list without the first elementHint: you can define an inner function if you need to pass extra parameters to your function.Testing: You can use the toList method to convert from a String to a List[Char]: e.g. "(just an) example".toList.Exercise 3: Counting ChangeWrite a recursive function that counts how many different ways you can make change for an amount, given a list of coin denominations. For example, there are 3 ways to give change for 4 if you have coins with denomiation 1 and 2: 1+1+1+1, 1+1+2, 2+2.Do this exercise by implementing the countChange function in Main.scala. This function takes an amount to change, and a list of unique denominations for the coins. Its signature is as follows:def countChange(money: Int, coins: List[Int]): IntOnce again, you can make use of functions isEmpty, head and tail on the list of integers coins.Hint: Think of the degenerate cases. How many ways can you give change for 0 CHF? How many ways can you give change for >0 CHF, if you have no coins?

源程序如下:
package recfunimport common._object Main {  def main(args: Array[String]) {    println("Pascal's Triangle")    for (row <- 0 to 10) {//change to 22 to check pascal if out of Int range      for (col <- 0 to row)        print(pascal(col, row) + " ")      println()    }  }  /**   * Exercise 1   */  def pascal(c: Int, r: Int): Int = {    //everytime mutiply a little num in case of overflow Int rangedef factB(c: Int, r: Int): Int =if (c==0) 1 else factB(c-1,r)*(r-c+1)/cfactB(c,r)  }  /**   * Exercise 2   */  def balance(chars: List[Char]): Boolean ={  def count(char:Char):Int ={  if (char == '(') 1  else if (char == ')') -1  else 0  }  def isOK(len:Int,chars:List[Char]):Boolean={  if (chars.isEmpty) len==0  else if (len==0 && count(chars.head) == -1) false  else isOK(len+count(chars.head),chars.tail)  }  isOK(0,chars)  }  /**   * Exercise 3   */  def countChange(money: Int, coins: List[Int]): Int =if (money==0) 1else if (coins.isEmpty || money<0) 0else countChange(money,coins.tail)+countChange(money-coins.head,coins)}