Last login: Wed Nov 25 12:27:02 on console /Applications/scala-2.7.7.final/bin/scala ; exit; howdy D-69-91-165-161:~ stuartreges$ /Applications/scala-2.7.7.final/bin/scala ; exit;Welcome to Scala version 2.7.7.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_15). Type in expressions to have them evaluated. Type :help for more information. scala> 2 + 2 res0: Int = 4 scala> 2 * 3 % 7 res1: Int = 6 scala> res1 res2: Int = 6 scala> 3.4 res3: Double = 3.4 scala> 2.toString res4: java.lang.String = 2 scala> 3.4.toString res5: java.lang.String = 3.4 scala> true.toString res6: java.lang.String = true scala> "hello" res7: java.lang.String = hello scala> 'c' res8: Char = c scala> val x = 2.2 * 3 x: Double = 6.6000000000000005 scala> x = 14.5 :5: error: reassignment to val x = 14.5 ^ scala> val x = "hello" x: java.lang.String = hello scala> x res10: java.lang.String = hello scala> var x = 3.4 x: Double = 3.4 scala> x = 14.5 x: Double = 14.5 scala> (x : Double) => x * x res12: (Double) => Double = scala> ((x : Double) => x * x)(4.2) res13: Double = 17.64 scala> val f = (x : Double) => x * x f: (Double) => Double = scala> f(3.4) res14: Double = 11.559999999999999 scala> val compare = (s1 : String, s2 : String) => s1.length > s2.length compare: (String, String) => Boolean = scala> compare("hello", "there") res15: Boolean = false scala> if (2 < 3) 14 else 72 res16: Int = 14 scala> if (2 > 3) 14 else 72 res17: Int = 72 scala> if (2 > 3) 14 else "foo" res18: Any = foo scala> if (2 > 3) 14 else 17.5 res19: AnyVal = 17.5 scala> if (2 > 3) 14 else true res20: AnyVal = true scala> def f(n : Int) = 2 * n f: (Int)Int scala> f(17) res21: Int = 34 scala> def fact(n : Int) = if (n == 0) 1 else n * fact(n - 1) :4: error: recursive method fact needs result type def fact(n : Int) = if (n == 0) 1 else n * fact(n - 1) ^ scala> def fact(n : Int) : Int = if (n == 0) 1 else n * fact(n - 1) fact: (Int)Int scala> fact(10) res22: Int = 3628800 scala> fact(15) res23: Int = 2004310016 scala> fact(240) res24: Int = 0 scala> def fact(n : BigInt) : BigInt = if (n == 0) 1 else n * fact(n - 1) fact: (BigInt)BigInt scala> fact(240) res25: BigInt = 40678853636470581204935759214868853101720512591828271460697559690814869189255851040091007297283485229238208902458700986591471560519057325631473815990984592447524630276881157053717046282863266212384565433072676086125451683377796691387594517603959682174236179543307370341645964969639868177222522210597680808524899409956055791719996669160040429652938967998005980799852641951... scala> x = 84903849023482390 :1: error: integer number too large x = 84903849023482390 ^ scala> Nil res26: Nil.type = List() scala> 3 :: Nil res27: List[Int] = List(3) scala> 1 :: 2 :: 3 :: Nil res28: List[Int] = List(1, 2, 3) scala> val lst = List(1, 3, 18, 47, 902, 2, -8) lst: List[Int] = List(1, 3, 18, 47, 902, 2, -8) scala> val lst2 = List(3, 84.5, 17, 19.2) lst2: List[AnyVal] = List(3, 84.5, 17, 19.2) scala> val lst2 = List(3, 84.5, 17, 19.2, "hello") lst2: List[Any] = List(3, 84.5, 17, 19.2, hello) scala> hd(lst) :6: error: not found: value hd hd(lst) ^ scala> lst.head res30: Int = 1 scala> lst.tail res31: List[Int] = List(3, 18, 47, 902, 2, -8) scala> lst.length res32: Int = 7 scala> lst res33: List[Int] = List(1, 3, 18, 47, 902, 2, -8) scala> lst2 res34: List[Any] = List(3, 84.5, 17, 19.2, hello) scala> lst ::: lst2 res35: List[Any] = List(1, 3, 18, 47, 902, 2, -8, 3, 84.5, 17, 19.2, hello) scala> def stutter(lst : List[Any]) = | if (lst.isEmpty) Nil else lst.head :: lst.head :: stutter(lst.tail) :6: error: recursive method stutter needs result type if (lst.isEmpty) Nil else lst.head :: lst.head :: stutter(lst.tail) ^ scala> def stutter(lst : List[Any]) : List[Any] = | if (lst.isEmpty) Nil else lst.head :: lst.head :: stutter(lst.tail) stutter: (List[Any])List[Any] scala> lst res36: List[Int] = List(1, 3, 18, 47, 902, 2, -8) scala> stutter(lst) res37: List[Any] = List(1, 1, 3, 3, 18, 18, 47, 47, 902, 902, 2, 2, -8, -8) scala> 2 + :5: error: ambiguous reference to overloaded definition, both method + in class Int of type (Double)Double and method + in class Int of type (Float)Float match expected type ? 2 + ^ scala> if (true) 3 else | 4 res39: Int = 3 scala> if (true) 3 scala> val x = if (true) 3 x: Unit = () scala> x scala> val x = if (false) 3 x: Unit = () scala> x scala> val x = if (false) then 3 :1: error: ';' expected but integer literal found. val x = if (false) then 3 ^ scala> val x = if (false) then 3; :1: error: ';' expected but integer literal found. val x = if (false) then 3; ^ scala> val x = if (false) 3; x: Unit = () scala> x scala> lst res44: List[Int] = List(1, 3, 18, 47, 902, 2, -8) scala> lst.map((x : Int) => x * x) res45: List[Int] = List(1, 9, 324, 2209, 813604, 4, 64) scala> lst.filter((x : Int) => x % 2 == 0) res46: List[Int] = List(18, 902, 2, -8) scala> lst.reduceLeft((x : Int, y : Int) => x + y) res47: Int = 965 scala> lst.sort((x : Int, y : Int) => x < y) res48: List[Int] = List(-8, 1, 2, 3, 18, 47, 902) scala> lst.sort((x : Int, y : Int) => x > y) res49: List[Int] = List(902, 47, 18, 3, 2, 1, -8) scala> (0 /: lst) {(x, y) => x + y} res50: Int = 965 scala> lst.partition((x : Int) => x < 10) res51: (List[Int], List[Int]) = (List(1, 3, 2, -8),List(18, 47, 902)) scala> def quicksort(lst : List[Int]) : List[Int] = | if (lst.length <= 1) lst else { | val (a, b) = lst.partition((x : Int) => x <= lst.head) | quicksort(a) ::: (lst.head :: quicksort(b)) | } quicksort: (List[Int])List[Int] scala> def quicksort(lst : List[Int]) : List[Int] = | if (lst.length <= 1) lst else { | val (a, b) = lst.tail.partition((x : Int) => x <= lst.head) | quicksort(a) ::: (lst.head :: quicksort(b)) | } quicksort: (List[Int])List[Int] scala> quicksort(lst) res52: List[Int] = List(-8, 1, 2, 3, 18, 47, 902) scala> lst(3) res53: Int = 47 scala> lst res54: List[Int] = List(1, 3, 18, 47, 902, 2, -8) scala> lst.slice(2, 5) res55: List[Int] = List(18, 47, 902) scala> 1 to 10 res56: Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> for (n <- 1 to 10) println n :5: error: value n is not a member of Unit for (n <- 1 to 10) println n ^ scala> for (n <- 1 to 10) println(n) 1 2 3 4 5 6 7 8 9 10 scala> for (n <- 1 to 10) println(n) 1 2 3 4 5 6 7 8 9 10 scala> ("blastoff" /: (1 to 10)) ((x, y) => y + ", " + x) res60: java.lang.String = 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, blastoff scala> (2, 3) res61: (Int, Int) = (2,3) scala> (2, 3.4) res62: (Int, Double) = (2,3.4) scala> (2, 3.4, "hello") res63: (Int, Double, java.lang.String) = (2,3.4,hello) scala> val (x, y, z) = (2, 3.4, "hello") x: Int = 2 y: Double = 3.4 z: java.lang.String = hello scala> class Data(val n: Int, var x : Double) defined class Data scala> val a = new Data :5: error: wrong number of arguments for constructor Data: (Int,Double)Data val a = new Data ^ scala> val a = new Data(13, 14.5) a: Data = Data@1515c044 scala> a.x res64: Double = 14.5 scala> a.n res65: Int = 13 scala> a.x_$eq(17.4) scala> a.x res67: Double = 17.4 scala> a.getClass.getMethods res68: Array[java.lang.reflect.Method] = Array(public double Data.x(), public int Data.$tag() throws java.rmi.RemoteException, public int Data.n(), public void Data.x_$eq(double), public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException, public final void java.lang.Object.wait() throws java.lang.InterruptedException, public final native void java.lang.Ob... scala> for (m <- a.getClass.getMethods) println(m) public double line85$object$$iw$$iw$Data.x() public int line85$object$$iw$$iw$Data.$tag() throws java.rmi.RemoteException public int line85$object$$iw$$iw$Data.n() public void line85$object$$iw$$iw$Data.x_$eq(double) public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException public final void java.lang.Object.wait() throws java.lang.InterruptedException public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException public native int java.lang.Object.hashCode() public final native java.lang.Class java.lang.Object.getClass() public boolean java.lang.Object.equals(java.lang.Object) public java.lang.String java.lang.Object.toString() public final native void java.lang.Object.notify() public final native void java.lang.Object.notifyAll() scala>