360
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
// Scala is about expressions
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
5 |
1 + 2
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
6 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
val r1 = 1 + 2
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
val r2 = r1 * r1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
10 |
val new_list =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
for (n <- (1 to 10).toList) yield n * n
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
14 |
def my_not_equal(x: Int, y: Int) : Boolean = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
!(x == y)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
16 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
19 |
// why return is not needed in Scala
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
20 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
21 |
def sum_even(ls: List[Int]): Int = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
22 |
val aux = for (x <- ls) yield {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
if (x % 2 == 0) x else 0
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
24 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
25 |
aux.sum
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
26 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
27 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
28 |
sum_even(List(1,2,3,4,5,6))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
29 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
30 |
def sum_return(ls: List[Int]): Int = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
31 |
val aux = for (x <- ls) yield {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
if (x % 2 == 0) (return x) else (return 0)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
33 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
34 |
aux.sum[Int]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
35 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
36 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
sum_return(List(2,3,4,5,6))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
39 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
40 |
// replace subexpressions should not
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
41 |
// change the meaning, but with return it does:
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
42 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
43 |
def sq1(n: Int): Int = n * n
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
44 |
def sq2(n: Int): Int = return n * n
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
45 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
46 |
def sum_squares(ls: List[Int]): Int = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
47 |
(for (n <- ls) yield (return n * n)).sum[Int]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
48 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
49 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
50 |
sum_squares(List(1,2,3,4,5,6)) |