-
Notifications
You must be signed in to change notification settings - Fork 5
/
01-scala-basics.scala
89 lines (71 loc) · 1.74 KB
/
01-scala-basics.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// val für Konstanten
// def für Methoden
// var für Variablen
abstract class Person(val name: String) {
// String name
// val name: String
def sayHello() = {
print("hello, " + name)
}
}
trait ByeBye {
def sayGoodbye = {
print("bye")
}
}
class Tillmann(name: String) extends Person(name) with ByeBye {
override def sayHello = {
print("hi")
}
}
object Me extends Tillmann("Tillmann")
trait UniPerson
case class Student(val matrikelnummer: Int) extends UniPerson
case class Professor(val fachgebiet: String) extends UniPerson
object PatternMatching {
def showUniPerson(p: UniPerson): String =
p match {
case Student(m) => "student nr " + m
case Professor(f) => "professor on " + f
}
def test = {
print(showUniPerson(Student(123)))
print(showUniPerson(Professor("programming languages")))
}
}
object AE {
// Abstract Syntax Tree
trait Exp
case class Num(n: Int) extends Exp
case class Add(lhs: Exp, rhs: Exp) extends Exp
// Example
val onePlusEight = Add(Num(1), Add(Num(5), Num(3)))
// Interpreter
def eval(e: Exp): Int =
e match {
case Num(n) => n
case Add(lhs, rhs) =>
eval(lhs) + eval(rhs)
}
}
// HOMEWORK
//
// Email homework as Scala source file to:
//
//
// Work in groups of 1 or 2 students.
//
// Put "pl1-hw01" in subject, please
//
// 0. write in the email:
// - your names
// - your student ids ("Matrikelnummer")
// - your study programme ("Studiengang")
// - how long you study ("Fachsemester")
// 1. install Scala
// 2. experiment with this file
// 3. add Subtraction
// 4. implement eval without pattern matching
//
// Send question by email to [email protected]