15
|
1 |
|
8
|
2 |
// webpages
|
14
|
3 |
|
|
4 |
|
|
5 |
//**Assignment (values)**
|
|
6 |
//=======================
|
|
7 |
val x = 42
|
|
8 |
val y = 3 + 4
|
|
9 |
|
|
10 |
|
|
11 |
//**Collections**
|
|
12 |
//===============
|
|
13 |
List(1,2,3,1)
|
|
14 |
Set(1,2,3,1)
|
|
15 |
|
|
16 |
1 to 10
|
|
17 |
(1 to 10).toList
|
|
18 |
|
|
19 |
(1 until 10).toList
|
|
20 |
|
|
21 |
|
|
22 |
//**Printing/Strings**
|
|
23 |
//====================
|
|
24 |
|
|
25 |
println("test")
|
15
|
26 |
|
|
27 |
|
14
|
28 |
val tst = "This is a " + "test"
|
|
29 |
println(tst)
|
|
30 |
|
|
31 |
val lst = List(1,2,3,1)
|
|
32 |
|
|
33 |
println(lst.toString)
|
|
34 |
println(lst.mkString("\n"))
|
|
35 |
|
|
36 |
// some methods take more than one argument
|
|
37 |
println(lst.mkString("[",",","]"))
|
|
38 |
|
|
39 |
//**Conversion methods**
|
|
40 |
//======================
|
|
41 |
|
|
42 |
List(1,2,3,1).toString
|
|
43 |
List(1,2,3,1).toSet
|
|
44 |
"hello".toList
|
|
45 |
1.toDouble
|
|
46 |
|
|
47 |
//**Types**
|
|
48 |
//=========
|
|
49 |
|
|
50 |
// Int, Long, BigInt
|
|
51 |
// String, Char
|
|
52 |
// List[Int], Set[Double]
|
|
53 |
// Pairs: (Int, String)
|
|
54 |
// List[(BigInt, String)]
|
12
|
55 |
|
|
56 |
|
14
|
57 |
//**Smart Strings**
|
|
58 |
//=================
|
|
59 |
""" """
|
|
60 |
|
|
61 |
//**Pairs/Tuples**
|
|
62 |
//================
|
|
63 |
|
|
64 |
val p = (1, "one")
|
|
65 |
p._1
|
|
66 |
p._2
|
|
67 |
|
|
68 |
val t = (4,1,2,3)
|
|
69 |
t._4
|
|
70 |
|
|
71 |
//**Function Definitions**
|
|
72 |
//========================
|
|
73 |
|
|
74 |
def square(x: Int): Int = x * x
|
|
75 |
|
|
76 |
//**Ifs control structures**
|
|
77 |
//==========================
|
|
78 |
|
|
79 |
def fact(n: Int): Int =
|
|
80 |
if (n == 0) 1 else n * fact(n - 1)
|
|
81 |
|
|
82 |
|
15
|
83 |
|
|
84 |
|
|
85 |
|
14
|
86 |
def fact2(n: BigInt): BigInt =
|
|
87 |
if (n == 0) 1 else n * fact2(n - 1)
|
|
88 |
|
|
89 |
def fib(n: Int): Int =
|
|
90 |
if (n == 0) 1 else
|
|
91 |
if (n == 1) 1 else fib(n - 1) + f(n - 2)
|
|
92 |
|
|
93 |
|
|
94 |
//a recursive function
|
|
95 |
def gcd(x: Int, y: Int): Int = 2 //???
|
|
96 |
|
15
|
97 |
//**String Interpolations**
|
|
98 |
//=========================
|
14
|
99 |
|
|
100 |
|
|
101 |
//**Assert/Testing**
|
|
102 |
====================
|
|
103 |
|
|
104 |
//**For-Maps (not For-Loops)**
|
|
105 |
//============================
|
|
106 |
|
|
107 |
for (n <- (1 to 10).toList) yield square(n)
|
|
108 |
|
|
109 |
for (n <- (1 to 10).toList; m <- (1 to 10).toList) yield m * n
|
|
110 |
|
|
111 |
val mtable = for (n <- (1 to 10).toList; m <- (1 to 10).toList) yield m * n
|
|
112 |
|
|
113 |
mtable.sliding(10,10).toList.mkString(
|