val count = 1 def foo(x : Int) : Int = { val y = x*x val z = y*y z*z } def twice(x: Int) = 2*x abstract class Color case class NamedColor(name: String) extends Color case class RGBColor(r: Int, g: Int, b: Int) extends Color def colorToStr(c: Color) : String = c match { case NamedColor(n) => n case RGBColor(r, g, b) => "#%02x%02x%02x".format(r, g, b) } def sum(xs: List[Int]) : Int = xs match { case Nil => 0 case x :: xs => x + sum (xs) } def iterate[T](a: T, f: (T) => T, p: (T) => Boolean) : List[T] = if (p(a)) Nil else a :: iterate(f(a), f, p) def sieve(xs: List[Int]) : List[Int] = xs match { case Nil => Nil case y::ys => ys.filter((z: Int) => z%y != 0) } def primes(n: Int) : List[Int] = { val from2 = iterate(2, (x: Int) => x+1, (x: Int) => x>n) val seqs = iterate(from2, sieve, (xs:List[Int]) => xs.isEmpty) seqs.map((xs:List[Int]) => xs.head) } /* def sieveAux(x: Int, y: Int, ys: List[Int]) : List[Int] = ys match { case Nil => Nil case z::zs if z z :: sieveAux(x, y, zs) case z::zs if y==z => sieveAux(x, x+y, zs) case z::zs => sieveAux(x, x+y, ys) } def primes(xs: List[Int]): List[Int] = xs match { case Nil => Nil case y :: ys => y :: primes(sieveAux(y, y, ys)) } */