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