21
|
1 |
// Lecture 1
|
25
|
2 |
//===========
|
14
|
3 |
|
26
|
4 |
// Value assignments
|
25
|
5 |
// (variable names should be lower case)
|
|
6 |
//======================================
|
21
|
7 |
|
14
|
8 |
val x = 42
|
|
9 |
val y = 3 + 4
|
|
10 |
|
|
11 |
|
25
|
12 |
// Collections
|
|
13 |
//=============
|
14
|
14 |
List(1,2,3,1)
|
|
15 |
Set(1,2,3,1)
|
|
16 |
|
|
17 |
1 to 10
|
|
18 |
(1 to 10).toList
|
|
19 |
|
|
20 |
(1 until 10).toList
|
|
21 |
|
18
|
22 |
// an element in a list
|
23
|
23 |
List(1, 2, 3, 1)(0)
|
|
24 |
List(1, 2, 3, 1)(3)
|
18
|
25 |
|
23
|
26 |
1::2::3::Nil
|
|
27 |
List(1, 2, 3) ::: List(4, 5, 6)
|
14
|
28 |
|
25
|
29 |
// Printing/Strings
|
|
30 |
//==================
|
14
|
31 |
|
|
32 |
println("test")
|
15
|
33 |
|
|
34 |
|
26
|
35 |
val tst = "This is a " ++ "test\n"
|
14
|
36 |
println(tst)
|
|
37 |
|
|
38 |
val lst = List(1,2,3,1)
|
|
39 |
|
|
40 |
println(lst.toString)
|
|
41 |
println(lst.mkString("\n"))
|
|
42 |
|
|
43 |
// some methods take more than one argument
|
21
|
44 |
println(lst.mkString("[", ",", "]"))
|
14
|
45 |
|
25
|
46 |
// Conversion methods
|
|
47 |
//====================
|
14
|
48 |
|
|
49 |
List(1,2,3,1).toString
|
|
50 |
List(1,2,3,1).toSet
|
|
51 |
"hello".toList
|
|
52 |
1.toDouble
|
|
53 |
|
25
|
54 |
|
|
55 |
List(1,2,3,4).reverse
|
|
56 |
|
|
57 |
// Types
|
|
58 |
//=======
|
|
59 |
|
|
60 |
/* Scala is a strongly typed language
|
|
61 |
|
|
62 |
* Base types
|
14
|
63 |
|
25
|
64 |
Int, Long, BigInt, Float, Double
|
|
65 |
String, Char
|
|
66 |
Boolean
|
|
67 |
|
|
68 |
* Compound types
|
12
|
69 |
|
25
|
70 |
List[Int],
|
|
71 |
Set[Double]
|
|
72 |
Pairs: (Int, String)
|
|
73 |
List[(BigInt, String)]
|
|
74 |
*/
|
12
|
75 |
|
25
|
76 |
// Smart Strings
|
|
77 |
//===============
|
|
78 |
|
23
|
79 |
println(">\n<")
|
|
80 |
println(""">\n<""")
|
|
81 |
|
|
82 |
/* in Java
|
|
83 |
val lyrics = "Baa, Baa, Black Sheep \n" +
|
|
84 |
"Have you any wool? \n" +
|
|
85 |
"Yes, sir, yes sir \n" +
|
|
86 |
"Three bags full"
|
|
87 |
*/
|
|
88 |
|
|
89 |
val lyrics = """Baa, Baa, Black Sheep
|
|
90 |
|Have you any wool?
|
|
91 |
|Yes, sir, yes sir
|
|
92 |
|Three bags full""".stripMargin
|
|
93 |
|
|
94 |
println(lyrics)
|
|
95 |
|
14
|
96 |
|
25
|
97 |
// Pairs/Tuples
|
|
98 |
//==============
|
14
|
99 |
|
|
100 |
val p = (1, "one")
|
|
101 |
p._1
|
|
102 |
p._2
|
|
103 |
|
|
104 |
val t = (4,1,2,3)
|
|
105 |
t._4
|
|
106 |
|
25
|
107 |
// Hello World
|
|
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 |
//======================
|
14
|
118 |
|
|
119 |
def square(x: Int): Int = x * x
|
|
120 |
|
25
|
121 |
square(6)
|
21
|
122 |
|
|
123 |
|
25
|
124 |
// If control structure
|
|
125 |
//======================
|
14
|
126 |
|
|
127 |
def fact(n: Int): Int =
|
|
128 |
if (n == 0) 1 else n * fact(n - 1)
|
|
129 |
|
25
|
130 |
/* boolean operators
|
|
131 |
|
|
132 |
== equals
|
|
133 |
! not
|
|
134 |
&& || and, or
|
|
135 |
*/
|
15
|
136 |
|
|
137 |
|
14
|
138 |
def fact2(n: BigInt): BigInt =
|
|
139 |
if (n == 0) 1 else n * fact2(n - 1)
|
|
140 |
|
25
|
141 |
fact2(150)
|
|
142 |
|
26
|
143 |
|
14
|
144 |
def fib(n: Int): Int =
|
|
145 |
if (n == 0) 1 else
|
26
|
146 |
if (n == 1) 1 else fib(n - 1) + fib(n - 2)
|
14
|
147 |
|
|
148 |
|
26
|
149 |
//gcd - Euclid's algorithm
|
|
150 |
|
|
151 |
def gcd(a: Int, b: Int): Int =
|
|
152 |
if (b == 0) a else gcd(b, a % b)
|
|
153 |
|
|
154 |
gcd(48, 18)
|
|
155 |
|
14
|
156 |
|
25
|
157 |
// String Interpolations
|
|
158 |
//=======================
|
14
|
159 |
|
26
|
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 |
|
14
|
174 |
|
25
|
175 |
// Assert/Testing
|
|
176 |
//================
|
14
|
177 |
|
26
|
178 |
// For-Comprehensions (not For-Loops)
|
|
179 |
//====================================
|
14
|
180 |
|
|
181 |
for (n <- (1 to 10).toList) yield square(n)
|
|
182 |
|
25
|
183 |
for (n <- (1 to 10).toList;
|
|
184 |
m <- (1 to 10).toList) yield m * n
|
21
|
185 |
|
|
186 |
|
26
|
187 |
val mult_table =
|
|
188 |
for (n <- (1 to 10).toList;
|
|
189 |
m <- (1 to 10).toList) yield m * n
|
|
190 |
|
|
191 |
mult_table.sliding(10,10).mkString("\n")
|
|
192 |
|
25
|
193 |
|
26
|
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 |
|
25
|
200 |
|
|
201 |
|
|
202 |
// Webpages
|
|
203 |
//==========
|