progs/lecture1.scala
changeset 25 6253f4681451
parent 23 8ac886bb0c15
child 26 a7afc2540a88
equal deleted inserted replaced
24:66b97f9a40f8 25:6253f4681451
     1 // Lecture 1
     1 // Lecture 1
     2 //==========
     2 //===========
     3 
     3 
     4 //**Assignments (values)**
     4 // Assignments (values)
     5 //(variable names should be lower case)
     5 // (variable names should be lower case)
     6 //=====================================
     6 //======================================
     7 
     7 
     8 val x = 42
     8 val x = 42
     9 val y = 3 + 4
     9 val y = 3 + 4
    10 
    10 
    11 
    11 
    12 //**Collections**
    12 // Collections
    13 //===============
    13 //=============
    14 List(1,2,3,1)
    14 List(1,2,3,1)
    15 Set(1,2,3,1)
    15 Set(1,2,3,1)
    16 
    16 
    17 1 to 10
    17 1 to 10
    18 (1 to 10).toList
    18 (1 to 10).toList
    24 List(1, 2, 3, 1)(3)
    24 List(1, 2, 3, 1)(3)
    25 
    25 
    26 1::2::3::Nil
    26 1::2::3::Nil
    27 List(1, 2, 3) ::: List(4, 5, 6)
    27 List(1, 2, 3) ::: List(4, 5, 6)
    28 
    28 
    29 //**Printing/Strings**
    29 // Printing/Strings
    30 //====================
    30 //==================
    31 
    31 
    32 println("test")
    32 println("test")
    33 
    33 
    34 
    34 
    35 val tst = "This is a " + "test" 
    35 val tst = "This is a " + "test" 
    41 println(lst.mkString("\n"))
    41 println(lst.mkString("\n"))
    42 
    42 
    43 // some methods take more than one argument
    43 // some methods take more than one argument
    44 println(lst.mkString("[", ",", "]"))
    44 println(lst.mkString("[", ",", "]"))
    45 
    45 
    46 //**Conversion methods**
    46 // Conversion methods
    47 //======================
    47 //====================
    48 
    48 
    49 List(1,2,3,1).toString
    49 List(1,2,3,1).toString
    50 List(1,2,3,1).toSet
    50 List(1,2,3,1).toSet
    51 "hello".toList
    51 "hello".toList
    52 1.toDouble
    52 1.toDouble
    53 
    53 
    54 //**Types**
       
    55 //=========
       
    56 
    54 
    57 // Int, Long, BigInt, Float, Double
    55 List(1,2,3,4).reverse
    58 // String, Char
       
    59 // List[Int], Set[Double]
       
    60 // Pairs: (Int, String)        
       
    61 // List[(BigInt, String)]
       
    62 
    56 
       
    57 // Types
       
    58 //=======
    63 
    59 
    64 //**Smart Strings**
    60 /* Scala is a strongly typed language
    65 //=================
    61  
       
    62  * Base types
       
    63 
       
    64     Int, Long, BigInt, Float, Double
       
    65     String, Char
       
    66     Boolean
       
    67 
       
    68  * Compound types 
       
    69 
       
    70     List[Int],
       
    71     Set[Double]
       
    72     Pairs: (Int, String)        
       
    73     List[(BigInt, String)]
       
    74 */
       
    75 
       
    76 // Smart Strings
       
    77 //===============
       
    78 
    66 println(">\n<")
    79 println(">\n<")
    67 println(""">\n<""")
    80 println(""">\n<""")
    68 
    81 
    69 /* in Java
    82 /* in Java
    70 val lyrics = "Baa, Baa, Black Sheep \n" +
    83 val lyrics = "Baa, Baa, Black Sheep \n" +
    79                 |Three bags full""".stripMargin
    92                 |Three bags full""".stripMargin
    80 
    93 
    81 println(lyrics)
    94 println(lyrics)
    82 
    95 
    83 
    96 
    84 //**Pairs/Tuples**
    97 // Pairs/Tuples
    85 //================
    98 //==============
    86 
    99 
    87 val p = (1, "one")
   100 val p = (1, "one")
    88 p._1
   101 p._1
    89 p._2
   102 p._2
    90 
   103 
    91 val t = (4,1,2,3)
   104 val t = (4,1,2,3)
    92 t._4
   105 t._4
    93 
   106 
    94 //**Function Definitions**
   107 // Hello World
    95 //========================
   108 //=============
       
   109 
       
   110 // show an example of a stand-alone scala file
       
   111 // remind that in the course work you are asked a 
       
   112 // plain scala "work-sheet"
       
   113 
       
   114 
       
   115 
       
   116 // Function Definitions
       
   117 //======================
    96 
   118 
    97 def square(x: Int): Int = x * x
   119 def square(x: Int): Int = x * x
    98 
   120 
       
   121 square(6)
    99 
   122 
   100 
   123 
   101 
   124 // If control structure
   102 //**Ifs control structures**
   125 //======================
   103 //==========================
       
   104 
   126 
   105 def fact(n: Int): Int = 
   127 def fact(n: Int): Int = 
   106   if (n == 0) 1 else n * fact(n - 1)
   128   if (n == 0) 1 else n * fact(n - 1)
   107 
   129 
   108 
   130 /* boolean operators
   109 
   131  
       
   132    ==     equals
       
   133    !      not
       
   134    && ||  and, or
       
   135    
       
   136 */
   110 
   137 
   111 
   138 
   112 def fact2(n: BigInt): BigInt = 
   139 def fact2(n: BigInt): BigInt = 
   113   if (n == 0) 1 else n * fact2(n - 1)
   140   if (n == 0) 1 else n * fact2(n - 1)
       
   141 
       
   142 fact2(150)
   114 
   143 
   115 def fib(n: Int): Int =
   144 def fib(n: Int): Int =
   116   if (n == 0) 1 else
   145   if (n == 0) 1 else
   117     if (n == 1) 1 else fib(n - 1) + f(n - 2)
   146     if (n == 1) 1 else fib(n - 1) + f(n - 2)
   118 
   147 
   119 
   148 
   120 //a recursive function
   149 //a recursive function
   121 def gcd(x: Int, y: Int): Int = 2 //??? 
   150 def gcd(x: Int, y: Int): Int = 2 //??? 
   122 
   151 
   123 //**String Interpolations**
   152 // String Interpolations
   124 //=========================
   153 //=======================
   125 
   154 
   126 
   155 
   127 //**Assert/Testing**
   156 // Assert/Testing
   128 ====================
   157 //================
   129 
   158 
   130 //**For-Maps (not For-Loops)**
   159 // For-Maps (not For-Loops)
   131 //============================
   160 //==========================
   132 
   161 
   133 for (n <- (1 to 10).toList) yield square(n)
   162 for (n <- (1 to 10).toList) yield square(n)
   134 
   163 
   135 for (n <- (1 to 10).toList; m <- (1 to 10).toList) yield m * n
   164 for (n <- (1 to 10).toList; 
   136 
   165      m <- (1 to 10).toList) yield m * n
   137 val mtable = for (n <- (1 to 10).toList; m <- (1 to 10).toList) yield m * n
       
   138 
       
   139 mtable.sliding(10,10).toList.mkString(
       
   140 
   166 
   141 
   167 
   142 // webpages
   168 val mtable = for (n <- (1 to 10).toList; 
       
   169                   m <- (1 to 10).toList) yield m * n
       
   170 
       
   171 mtable.sliding(10,10).mkString("\n")
       
   172 
       
   173 
       
   174 // Webpages
       
   175 //==========