progs/lecture1.scala
changeset 13 0ce25f816414
parent 12 4703aebadd23
child 14 2d9defa8ad7f
equal deleted inserted replaced
12:4703aebadd23 13:0ce25f816414
     2 // function definition
     2 // function definition
     3 // smart strings
     3 // smart strings
     4 // maps
     4 // maps
     5 // recursion
     5 // recursion
     6 // webpages
     6 // webpages
       
     7 // string interpolations
     7 
     8 
     8 
     9 
     9 //**Assignment (values)**
       
    10 //=======================
       
    11 val x = 42
       
    12 val y = 3 + 4
       
    13 
       
    14 
       
    15 //**Collections**
       
    16 //===============
       
    17 List(1,2,3,1)
       
    18 Set(1,2,3,1)
       
    19 
       
    20 1 to 10
       
    21 (1 to 10).toList
       
    22 
       
    23 (1 until 10).toList
       
    24 
       
    25 
       
    26 //**Printing/Strings**
       
    27 //====================
       
    28 
       
    29 println("test")
       
    30 val tst = "This is a " + "test" 
       
    31 println(tst)
       
    32 
       
    33 val lst = List(1,2,3,1)
       
    34 
       
    35 println(lst.toString)
       
    36 println(lst.mkString("\n"))
       
    37 
       
    38 // some methods take more than one argument
       
    39 println(lst.mkString("[",",","]"))
       
    40 
       
    41 //**Conversion methods**
       
    42 //======================
       
    43 
       
    44 List(1,2,3,1).toString
       
    45 List(1,2,3,1).toSet
       
    46 "hello".toList
       
    47 1.toDouble
       
    48 
       
    49 //**Types**
       
    50 //=========
       
    51 
       
    52 // Int, Long, BigInt
       
    53 // String, Char
       
    54 // List[Int], Set[Double]
       
    55 // Pairs: (Int, String)        
       
    56 // List[(BigInt, String)]
       
    57 
       
    58 
       
    59 //**Smart Strings**
       
    60 //=================
       
    61 
       
    62 
       
    63 //**Pairs/Tuples**
       
    64 //================
       
    65 
       
    66 val p = (1, "one")
       
    67 p._1
       
    68 p._2
       
    69 
       
    70 val t = (4,1,2,3)
       
    71 t._4
       
    72 
       
    73 //**Function Definitions**
       
    74 //========================
       
    75 
       
    76 def square(x: Int): Int = x * x
       
    77 
       
    78 //**Ifs control structures**
       
    79 //==========================
       
    80 
       
    81 def fact(n: Int): Int = 
       
    82   if (n == 0) 1 else n * fact(n - 1)
       
    83 
       
    84 
       
    85 def fact2(n: BigInt): BigInt = 
       
    86   if (n == 0) 1 else n * fact2(n - 1)
       
    87 
       
    88 def fib(n: Int): Int =
       
    89   if (n == 0) 1 else
       
    90     if (n == 1) 1 else fib(n - 1) + f(n - 2)
       
    91 
       
    92 
       
    93 //a recursive function
       
    94 def gcd(x: Int, y: Int): Int = 2 //??? 
       
    95 
       
    96 
       
    97 
       
    98 //**Assert/Testing**
       
    99 ====================
       
   100 
       
   101 //**For-Maps (not For-Loops)**
       
   102 //============================
       
   103 
       
   104 for (n <- (1 to 10).toList) yield square(n)
       
   105 
       
   106 for (n <- (1 to 10).toList; m <- (1 to 10).toList) yield m * n
       
   107 
       
   108 val mtable = for (n <- (1 to 10).toList; m <- (1 to 10).toList) yield m * n
       
   109 
       
   110 mtable.sliding(10,10).toList.mkString(