21
|
1 |
// Lecture 1
|
|
2 |
//==========
|
14
|
3 |
|
18
|
4 |
//**Assignments (values)**
|
|
5 |
//(variable names should be lower case)
|
|
6 |
//=====================================
|
21
|
7 |
|
14
|
8 |
val x = 42
|
|
9 |
val y = 3 + 4
|
|
10 |
|
|
11 |
|
|
12 |
//**Collections**
|
|
13 |
//===============
|
|
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 |
|
|
29 |
//**Printing/Strings**
|
|
30 |
//====================
|
|
31 |
|
|
32 |
println("test")
|
15
|
33 |
|
|
34 |
|
14
|
35 |
val tst = "This is a " + "test"
|
|
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 |
|
|
46 |
//**Conversion methods**
|
|
47 |
//======================
|
|
48 |
|
|
49 |
List(1,2,3,1).toString
|
|
50 |
List(1,2,3,1).toSet
|
|
51 |
"hello".toList
|
|
52 |
1.toDouble
|
|
53 |
|
|
54 |
//**Types**
|
|
55 |
//=========
|
|
56 |
|
23
|
57 |
// Int, Long, BigInt, Float, Double
|
14
|
58 |
// String, Char
|
|
59 |
// List[Int], Set[Double]
|
|
60 |
// Pairs: (Int, String)
|
|
61 |
// List[(BigInt, String)]
|
12
|
62 |
|
|
63 |
|
14
|
64 |
//**Smart Strings**
|
|
65 |
//=================
|
23
|
66 |
println(">\n<")
|
|
67 |
println(""">\n<""")
|
|
68 |
|
|
69 |
/* in Java
|
|
70 |
val lyrics = "Baa, Baa, Black Sheep \n" +
|
|
71 |
"Have you any wool? \n" +
|
|
72 |
"Yes, sir, yes sir \n" +
|
|
73 |
"Three bags full"
|
|
74 |
*/
|
|
75 |
|
|
76 |
val lyrics = """Baa, Baa, Black Sheep
|
|
77 |
|Have you any wool?
|
|
78 |
|Yes, sir, yes sir
|
|
79 |
|Three bags full""".stripMargin
|
|
80 |
|
|
81 |
println(lyrics)
|
|
82 |
|
14
|
83 |
|
|
84 |
//**Pairs/Tuples**
|
|
85 |
//================
|
|
86 |
|
|
87 |
val p = (1, "one")
|
|
88 |
p._1
|
|
89 |
p._2
|
|
90 |
|
|
91 |
val t = (4,1,2,3)
|
|
92 |
t._4
|
|
93 |
|
|
94 |
//**Function Definitions**
|
|
95 |
//========================
|
|
96 |
|
|
97 |
def square(x: Int): Int = x * x
|
|
98 |
|
21
|
99 |
|
|
100 |
|
|
101 |
|
14
|
102 |
//**Ifs control structures**
|
|
103 |
//==========================
|
|
104 |
|
|
105 |
def fact(n: Int): Int =
|
|
106 |
if (n == 0) 1 else n * fact(n - 1)
|
|
107 |
|
|
108 |
|
15
|
109 |
|
|
110 |
|
|
111 |
|
14
|
112 |
def fact2(n: BigInt): BigInt =
|
|
113 |
if (n == 0) 1 else n * fact2(n - 1)
|
|
114 |
|
|
115 |
def fib(n: Int): Int =
|
|
116 |
if (n == 0) 1 else
|
|
117 |
if (n == 1) 1 else fib(n - 1) + f(n - 2)
|
|
118 |
|
|
119 |
|
|
120 |
//a recursive function
|
|
121 |
def gcd(x: Int, y: Int): Int = 2 //???
|
|
122 |
|
15
|
123 |
//**String Interpolations**
|
|
124 |
//=========================
|
14
|
125 |
|
|
126 |
|
|
127 |
//**Assert/Testing**
|
|
128 |
====================
|
|
129 |
|
|
130 |
//**For-Maps (not For-Loops)**
|
|
131 |
//============================
|
|
132 |
|
|
133 |
for (n <- (1 to 10).toList) yield square(n)
|
|
134 |
|
|
135 |
for (n <- (1 to 10).toList; m <- (1 to 10).toList) yield m * n
|
|
136 |
|
|
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(
|
21
|
140 |
|
|
141 |
|
|
142 |
// webpages
|