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\n" |
36 println(tst) |
36 println(tst) |
37 |
37 |
38 val lst = List(1,2,3,1) |
38 val lst = List(1,2,3,1) |
39 |
39 |
40 println(lst.toString) |
40 println(lst.toString) |
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** |
54 |
55 //========= |
55 List(1,2,3,4).reverse |
56 |
56 |
57 // Int, Long, BigInt, Float, Double |
57 // Types |
58 // String, Char |
58 //======= |
59 // List[Int], Set[Double] |
59 |
60 // Pairs: (Int, String) |
60 /* Scala is a strongly typed language |
61 // List[(BigInt, String)] |
61 |
62 |
62 * Base types |
63 |
63 |
64 //**Smart Strings** |
64 Int, Long, BigInt, Float, Double |
65 //================= |
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 |
99 |
121 square(6) |
100 |
122 |
101 |
123 |
102 //**Ifs control structures** |
124 // If control structure |
103 //========================== |
125 //====================== |
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 */ |
110 |
136 |
111 |
137 |
112 def fact2(n: BigInt): BigInt = |
138 def fact2(n: BigInt): BigInt = |
113 if (n == 0) 1 else n * fact2(n - 1) |
139 if (n == 0) 1 else n * fact2(n - 1) |
114 |
140 |
|
141 fact2(150) |
|
142 |
|
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) + fib(n - 2) |
118 |
147 |
119 |
148 |
120 //a recursive function |
149 //gcd - Euclid's algorithm |
121 def gcd(x: Int, y: Int): Int = 2 //??? |
150 |
122 |
151 def gcd(a: Int, b: Int): Int = |
123 //**String Interpolations** |
152 if (b == 0) a else gcd(b, a % b) |
124 //========================= |
153 |
125 |
154 gcd(48, 18) |
126 |
155 |
127 //**Assert/Testing** |
156 |
128 ==================== |
157 // String Interpolations |
129 |
158 //======================= |
130 //**For-Maps (not For-Loops)** |
159 |
131 //============================ |
160 val n = 3 |
|
161 println("The square of " + n + " is " + square(n) + ".") |
|
162 |
|
163 println(s"The square of ${n} is ${square(n)}.") |
|
164 |
|
165 |
|
166 |
|
167 def gcd_db(a: Int, b: Int): Int = { |
|
168 println(s"Function called with ${a} and ${b}.") |
|
169 if (b == 0) a else gcd_db(b, a % b) |
|
170 } |
|
171 |
|
172 gcd_db(48, 18) |
|
173 |
|
174 |
|
175 // Assert/Testing |
|
176 //================ |
|
177 |
|
178 // For-Comprehensions (not For-Loops) |
|
179 //==================================== |
132 |
180 |
133 for (n <- (1 to 10).toList) yield square(n) |
181 for (n <- (1 to 10).toList) yield square(n) |
134 |
182 |
135 for (n <- (1 to 10).toList; m <- (1 to 10).toList) yield m * n |
183 for (n <- (1 to 10).toList; |
136 |
184 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 |
185 |
138 |
186 |
139 mtable.sliding(10,10).toList.mkString( |
187 val mult_table = |
140 |
188 for (n <- (1 to 10).toList; |
141 |
189 m <- (1 to 10).toList) yield m * n |
142 // webpages |
190 |
|
191 mult_table.sliding(10,10).mkString("\n") |
|
192 |
|
193 |
|
194 // with patterns |
|
195 |
|
196 for ((m, n) <- List((1, 4), (2, 3), (3, 2), (4, 1))) yield m + n |
|
197 |
|
198 for (p <- List((1, 4), (2, 3), (3, 2), (4, 1))) yield p._1 + p._2 |
|
199 |
|
200 |
|
201 |
|
202 // Webpages |
|
203 //========== |