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