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 //========== |