import scala.collection.immutable.ListMap /* Part 2 */ def incr(x: Int): Int = x + 1 def double(x: Int): Int = x + x def square(x: Int): Int = x * x def factorial(n: Int): Int = if (n == 0) {1} else {n * factorial(n-1)} def power(x: Int, n: Int): Int = if (n == 0) {1} else {x * power(x,n-1)} def factorial1(n: Int): Int = { val m = n-1 ; if (n == 0) {1} else {n * factorial1(m)} } def factorial2(n: Int): Int = { val m = n-1; if (n == 0) {1} else {n * factorial2(m)} } def factorial3(n: Int): Int = { val m = n-1; if (n == 0) { return 1; } else { return n * factorial3(m); } } /* Exercise 1 */ def p(x: Int, y:Int): Int = sys.error("todo") /* Exercise 2 */ def sum(n: Int): Int = sys.error("todo") /* Part 3 */ /* Exercise 3 */ def cycle(q:(Int,Int,Int)): (Int,Int,Int) = sys.error("todo") // alternative form: // def cycle(x: Int,y: Int,z: Int): (Int,Int,Int) = sys.error("todo") /* Part 4 */ def nameFromNum(presidentNum: Int): String = presidentNum match { case 41 => "George H. W. Bush" case 42 => "Bill Clinton" case 43 => "George W. Bush" case 44 => "Barack Obama" case n => "I don't know who president number " + n + " is" } def numFromName(presidentName: String): Int = presidentName match { case "George H. W. Bush" => 41 case "Bill Clinton" => 42 case "George W. Bush" => 43 case "Barack Obama" => 44 } /* Exercise 4 */ def suffix(n: Int): String = sys.error("todo") abstract class Colour case class Red() extends Colour case class Green() extends Colour case class Blue() extends Colour /* Exercise 5 */ def favouriteColour(c: Colour): Boolean = c match { case Red() => sys.error("todo") case Blue() => sys.error("todo") case Green() => sys.error("todo") } abstract class Shape case class Circle(r: Double, x: Double, y: Double) extends Shape case class Rectangle(llx: Double, lly: Double, w:Double, h:Double) extends Shape def center(s: Shape): (Double,Double) = s match { case Rectangle(llx,lly,w,h) => (llx+w/2, lly+h/2) case Circle(r,x,y) => (x,y) } /* Exercise 6 */ def boundingBox(s: Shape): Rectangle = sys.error("todo") /* Exercise 7 */ def mayOverlap(s1: Shape, s2: Shape) = sys.error("todo") /* Part 5 */ val anonIncr = {x: Int => x+1} // anonymous version of incr val anonAdd = {x: Int => {y: Int => x + y}} /* Exercise 8 */ def compose1[A, B, C](f: A => B, g: B => C)(x: A) = sys.error("todo") /* Exercise 9 */ def compose[A, B, C](f: A => B, g: B => C) = sys.error("todo") /* Exercise 10 */ def e1 = sys.error("todo") def e2 = sys.error("todo") def isEmpty[A](l: List[A]) = l match { case Nil => true case x :: y => false } /* Exercise 11 */ def map[A, B](f: A => B, l: List[A]): List[B] = sys.error("todo") /* Exercise 12 */ def filter[A](f: A => Boolean, l: List[A]): List[A] = sys.error("todo") /* Exercise 13 */ def reverse[A](l: List[A]): List[A] = sys.error("todo") /* Part 6 */ def empty[K,V]: List[(K,V)] = List() /* Exercise 14 */ def lookup[K, V](m: List[(K, V)], k: K): V = sys.error("todo") /* Exercise 15 */ def update[K, V](m: List[(K, V)], k: K, v: V): List[(K, V)] = sys.error("todo") /* Exercise 16 */ def keys[K,V](m: List[(K,V)]): List[K] = sys.error("todo") /* Exercise 17 */ val presidentListMap = ListMap() // TODO /* Exercise 18 */ def m0_withUpdate = sys.error("todo") /* Exercise 19 */ def list2map[K,V](l: List[(K,V)]): ListMap[K,V] = sys.error("todo") /* Exercise 20 */ def election(votes: List[String]): ListMap[String,Int] = sys.error("todo")